Fetching actor from server.

Hello, I'm trying to make a plugin that allows players to download actordata from a remote server.

I have set up
a function to revrieve the data sent to the server as a JSON string
JavaScript:
    HP_SoulsLikeSummon.RetrieveData = function(){
        console.log('Retrieving data from server'); //log if the function is running
        fetch(HP_SoulsLikeSummon.ServerUrl + '/get-json')
        .then(response => response.json())
        .then(data => {
          var actorData = data.system.variables._data;
            $gameVariables.setValue(HP_SoulsLikeSummon.Download, actorData);
            $gameActors._data[HP_SoulsLikeSummon.ReplacePlayerID] = $gameVariables.value(HP_SoulsLikeSummon.Download);
            
        console.log('Success:', data.system.variables._data); //log the outcome
        })
        .catch((error) => console.error('Error:', error)); // error display
    }

Send it to a variable and the apply it to a actorID 9.
It sort of works, but when I check the actorID with F8 it's not properly set up, it looks like this

image_2024-04-10_125413063.png


And as soon as I add the character to the party the game crashes.
I've been walking in circles all night trying fix it, but I haven't managed to.

I followed the code written here, it works when i store the data offline and push it to a actor but not online.

Thanks in advance for the help.
Did you write this code?

var actorData = data.system.variables._data;
$gameVariables.setValue(HP_SoulsLikeSummon.Download, actorData);
$gameActors._data[HP_SoulsLikeSummon.ReplacePlayerID] = $gameVariables.value(HP_SoulsLikeSummon.Download);
Without any other context, this looks weird.

Are you sure HP_SoulsLikeSummon.Download is an integer value? Obviously it is or you wouldn't be getting any data in your array at the end.

Why waste a line storing the actor data in a game variable for all of one statement before copying it into the $gameActors array instead of just putting it there in the first place?

Where did this data.system.variables._data come from and get structured? From the results of your console, it's clearly not cast as a Game_Actor object.
Did you write this code?


Without any other context, this looks weird.

Are you sure HP_SoulsLikeSummon.Download is an integer value? Obviously it is or you wouldn't be getting any data in your array at the end.

Why waste a line storing the actor data in a game variable for all of one statement before copying it into the $gameActors array instead of just putting it there in the first place?

Where did this data.system.variables._data come from and get structured? From the results of your console, it's clearly not cast as a Game_Actor object.
I wrote the code myself I'm learning java as I go.

I thought i had to store the data in a variable before transfering it to the actor array.

This is how the server receives the data from the player when they use the upload function.

JSON:
{
  "system": {
    "variables": {
      "_data": {
        "_hp": 450,
        "_mp": 200,
        "_tp": 0,
        "_hidden": false,
        "_paramPlus": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_states": [],
        "_stateTurns": {},
        "_stateSteps": {},
        "_buffs": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_buffTurns": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_stateCounter": {},
        "_turnBarrier": [],
        "_permBarrier": 0,
        "_actions": [],
        "_speed": 0,
        "_result": {
          "used": false,
          "missed": false,
          "evaded": false,
          "physical": false,
          "drain": false,
          "critical": false,
          "success": false,
          "hpAffected": false,
          "hpDamage": 0,
          "mpDamage": 0,
          "tpDamage": 0,
          "addedStates": [],
          "removedStates": [],
          "addedBuffs": [],
          "addedDebuffs": [],
          "removedBuffs": []
        },
        "_actionState": "",
        "_lastTargetIndex": 0,
        "_animations": [],
        "_damagePopup": false,
        "_effectType": null,
        "_motionType": null,
        "_weaponImageId": 0,
        "_motionRefresh": false,
        "_selected": false,
        "_actorId": 1,
        "_name": "Ayo",
        "_nickname": "Ayo",
        "_classId": 12,
        "_level": 1,
        "_characterName": "Walk",
        "_characterIndex": 0,
        "_faceName": "character",
        "_faceIndex": 0,
        "_battlerName": "MainBattler",
        "_exp": {
          "12": 0
        },
        "_skills": [],
        "_equips": [
          {
            "_dataClass": "weapon",
            "_itemId": 3001
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          },
          {
            "_dataClass": "",
            "_itemId": 0
          }
        ],
        "_actionInputIndex": 0,
        "_lastMenuSkill": {
          "_dataClass": "",
          "_itemId": 0
        },
        "_lastBattleSkill": {
          "_dataClass": "",
          "_itemId": 0
        },
        "_lastCommandSymbol": "",
        "_paramAllocationTimes": {
          "12": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ]
        },
        "_xparamAllocationTimes": {},
        "_sparamAllocationTimes": {},
        "_bonusAllocationPoints": {},
        "_spentAllocationPoints": {},
        "_summons": [],
        "_distributePoints": 0,
        "_pointsGained": 0,
        "_pointsGainedTemp": 0,
        "_distributePointsSpent": 0,
        "_distributeParams": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_distributeXParams": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_distributeSParams": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_profile": "the main character.",
        "_battleSkills": [
          0,
          0,
          0,
          0,
          0,
          0
        ],
        "_checkPassiveStateCondition": [],
        "_passiveStatesRaw": [],
        "_battleSkillMaxPlus": 0,
        "_cacheParamBuffRate": {
          "0": 1,
          "1": 1
        },
        "_locked": false,
        "_required": false,
        "_deathSounds": [
          [
            "oof",
            70,
            100
          ],
          [
            "oof2",
            70,
            100
          ]
        ],
        "_offsetmsgY": -40
      }
    }
  }
}

I thought I had to structure it the same way the engine stores data.

How can I cast it as a Game_Actor object?
It's JavaScript, a.k.a. ECMAScript; not Java.

The JSON format doesn't natively encode the prototype of an object; you have to manually restore that. MV's JsonEx wrapper has a method for that:
JavaScript:
JsonEx._resetPrototype(obj, Game_Actor.prototype);
Also, as Turan says, there does not appear to be any need to store your data in $gameVariables.
It's JavaScript, a.k.a. ECMAScript; not Java.

The JSON format doesn't natively encode the prototype of an object; you have to manually restore that. MV's JsonEx wrapper has a method for that:
JavaScript:
JsonEx._resetPrototype(obj, Game_Actor.prototype);
Also, as Turan says, there does not appear to be any need to store your data in $gameVariables.
Thanks for the assistance, i tried using it like this:

JavaScript:
/// code to get data from the server
    HP_SoulsLikeSummon.RetrieveData = function(){
        console.log('Retrieving data from server'); //log if the function is running
        fetch(HP_SoulsLikeSummon.ServerUrl + '/get-json')
        .then(response => response.json())
        .then(data => {
          var actorData = data.system.variables._data;
            //$gameVariables.setValue(HP_SoulsLikeSummon.Download, actorData);
            $gameActors._data[HP_SoulsLikeSummon.ReplacePlayerID] = JsonEx._resetPrototype(actorData, Game_Actor.prototype);
            
        console.log('Success:', data.system.variables._data); //log the outcome
        })
        .catch((error) => console.error('Error:', error)); // error display
    }

But once again I get a crash, although data seems to load correctly this time?
1712760234838.png


And this is the error I get

1712760254246.png


Thanks in advance
Why not just read your JSON from the server into the $dataActors object instead of drawing it from the local json file? That way the engine will otherwise work exactly the same way as it usually does and you won't need any further data manipulation.
Why not just read your JSON from the server into the $dataActors object instead of drawing it from the local json file? That way the engine will otherwise work exactly the same way as it usually does and you won't need any further data manipulation.
I don't know how to do it, and I thought that this was the best way of doing it.
I don't know how to do it, and I thought that this was the best way of doing it.
Assuming you're returning the data in the same format as it's used in the original json file, it should be as simple as doing

$dataActors = actorData;
And this is the error I get
I forgot about that; each Game_Item instance (equipped items) needs to have its prototype set, too. There may be other cases like that. 😴

MV's core scripts implement a pair of methods for this (cf rpg_core.js):
  • JsonEx.stringify is like JSON.stringify, but it also stores the constructor name (global).
  • JsonEx.parse is like JSON.parse, but it also restores the prototype if present.
I think the simplest option would be to use JsonEx.stringify to encode data before uploading, then JsonEx.parse to decode it after downloading. That's what normally happens for save files.
I forgot about that; each Game_Item instance (equipped items) needs to have its prototype set, too. There may be other cases like that. 😴

MV's core scripts implement a pair of methods for this (cf rpg_core.js):
  • JsonEx.stringify is like JSON.stringify, but it also stores the constructor name (global).
  • JsonEx.parse is like JSON.parse, but it also restores the prototype if present.
I think the simplest option would be to use JsonEx.stringify to encode data before uploading, then JsonEx.parse to decode it after downloading. That's what normally happens for save files.
Thank you, now it works as intended!
I can't help you with this, but I am curious as to why you want to do it? (not sure if this comes off as rude or not. It's a genuine question)
I can't help you with this, but I am curious as to why you want to do it? (not sure if this comes off as rude or not. It's a genuine question)
I want to make something similar to Dragons dogma pawns, where players are able to send their characters to other players.
At first it was supposed to be like dark souls but I decided to simplify it

Original thread: https://forums.rpgmakerweb.com/threads/fetching-actor-from-server.167627/