MZ Creating a custom property for an Actor.

Hi. I'm looking to save data into actors using a custom property (which I'm calling "._chardata") in JS. I've looked around and tried this code but it always returns null, and if I directly try to write into the "._chardata" property using a JS event in RPG Make MZ itself it says it "cannot read property "_chardata" of null."


JavaScript:
(function () {
    Object.defineProperties(Game_Actor.prototype, {
        chardata: {
            get: function () { return this._chardata || null; },
            set: function (value) { this._chardata = value; },
            configurable: true
        }

It's confusing cause it seems to be recognizing the property, or it wouldn't even return null, but i can't seem to get it to have the data I want on it for some reason.
It's hard to say what might be wrong without seeing the context of your code. For example, what you've pasted here should be throwing errors in the console because you're missing two closing braces.

Also this:
it seems to be recognizing the property, or it wouldn't even return null
is not necessarily true. Your getter is still returning null if the property doesn't exist at all, which makes it hard to troubleshoot whether it actually was assigned a null value at some point or is entirely undefined.

if I directly try to write into the "._chardata" property using a JS event in RPG Make MZ itself it says it "cannot read property "_chardata" of null."
That has nothing to do with your _chardata property, it's telling you the reference you're using for an actor is null. That's why we'd need to see the actual code you're trying to use this in.

Note that all of this is a bit unnecessary. You can simply assign a value to a property any time you like and check for it any time you like.

Especially since your getter isn't actually doing anything (e.g. assigning a minimum/default value the way actor parameters have), you still would need to check for it to be a valid value the same way you would if you didn't have the getter at all.
It's hard to say what might be wrong without seeing the context of your code. For example, what you've pasted here should be throwing errors in the console because you're missing two closing braces.

Also this:

is not necessarily true. Your getter is still returning null if the property doesn't exist at all, which makes it hard to troubleshoot whether it actually was assigned a null value at some point or is entirely undefined.


That has nothing to do with your _chardata property, it's telling you the reference you're using for an actor is null. That's why we'd need to see the actual code you're trying to use this in.

Note that all of this is a bit unnecessary. You can simply assign a value to a property any time you like and check for it any time you like.

Especially since your getter isn't actually doing anything (e.g. assigning a minimum/default value the way actor parameters have), you still would need to check for it to be a valid value the same way you would if you didn't have the getter at all.
I'm trying to do that but I'm having trouble. This is the larger code im trying to use to create an actor. it will not add the property and it comes up "undefined" if I try to read it. newChar is an object. I tried adding it as part of the actual actor object that gets put in the dataActors array, then I tried assigning it to the $dataActors[newCharActorData.id] part (using $dataActors[newCharActorData.id].chardata = newChar😉 now I'm trying to do it how you'd do it in the actual game using a scripting event by using $gameActors.actor(newCharActorData.id).chardata = newChar; I tried with and without the getter and setter code, too.

JavaScript:
PluginManager.registerCommand(PLUGIN_NAME, 'newChar', function newCharGenerate() {
   let newChar = new characterProfile();
   CHARACTERS.push(newChar);
   var newCharActorData = {
      
      "id": $dataActors.length + 1,
      "traits":[],
      "initialLevel":1,
      "maxLevel":99,
      "profile":newChar._age + " " + newChar._sex + " " + newChar._traits,
      "battlerName":"",
      "characterIndex":1,
      "characterName":"",
      "faceIndex":1,
      "faceName":"",
      "name":newChar._firstname + " " + newChar._lastname,
      "nickname":"",
      "note":"",
      "classId":1,
      "equips": [],
  
   };

   alert(newCharActorData.id);
  
   $dataActors[newCharActorData.id] = newCharActorData;

   if (!$gameActors.actor(newCharActorData.id)) {
    $gameActors.setup(newCharActorData.id);

    $gameActors.actor(newCharActorData.id).chardata = newChar;
}

I'm trying to do that but I'm having trouble. This is the larger code im trying to use to create an actor. it will not add the property and it comes up "undefined" if I try to read it. newChar is an object. I tried adding it as part of the actual actor object that gets put in the dataActors array, then I tried assigning it to the $dataActors[newCharActorData.id] part (using $dataActors[newCharActorData.id].chardata = newChar😉 now I'm trying to do it how you'd do it in the actual game using a scripting event by using $gameActors.actor(newCharActorData.id).chardata = newChar; I tried with and without the getter and setter code, too.

JavaScript:
PluginManager.registerCommand(PLUGIN_NAME, 'newChar', function newCharGenerate() {
   let newChar = new characterProfile();
   CHARACTERS.push(newChar);
   var newCharActorData = {
     
      "id": $dataActors.length + 1,
      "traits":[],
      "initialLevel":1,
      "maxLevel":99,
      "profile":newChar._age + " " + newChar._sex + " " + newChar._traits,
      "battlerName":"",
      "characterIndex":1,
      "characterName":"",
      "faceIndex":1,
      "faceName":"",
      "name":newChar._firstname + " " + newChar._lastname,
      "nickname":"",
      "note":"",
      "classId":1,
      "equips": [],
 
   };

   alert(newCharActorData.id);
 
   $dataActors[newCharActorData.id] = newCharActorData;

   if (!$gameActors.actor(newCharActorData.id)) {
    $gameActors.setup(newCharActorData.id);

    $gameActors.actor(newCharActorData.id).chardata = newChar;
}
I just realized setup might be doing it but it was still doing it when I was trying to make the actor have a .chardata property by default so idk.

I just realized setup might be doing it but it was still doing it when I was trying to make the actor have a .chardata property by default so idk.
nevermind its not doing it, it runs before the code that assigns the value so idk

Nevermind. I fixed it. It was dumb. The script I had to read the information was just pulling from too early in the array by 1 so it was just pulling a non-existent actor. Man.
That's still not complete code, it's missing closing braces. Presuming you actually have them and just didn't paste it into your post correctly...

Edit: saw you managed to figure something out as I was typing. Good luck.

@Fringo, please avoid double posting, as it is against the forum rules. You can use the "Edit" function on your posts to add additional information you've forgotten or respond to multiple people. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-custom-property-for-an-actor.182307/