MZ My Plugin's data is not loading from a save.

Hello!

Not quite sure where to put this but I created a plugin for my game, where in the new game everything loads fine, however when I load a save where the data should already be there, the data gets reset and I'm thoroughly confused why.

I'm using Scene_Boot:

Spoiler: Scene_Boot
Code:
PurpsMZ.TreeSystem.Scene_Boot_onDatabaseLoaded = Scene_Boot.prototype.onDatabaseLoaded;

//            prototype.onDatabaseLoaded
Scene_Boot.prototype.onDatabaseLoaded = function(){
    PurpsMZ.TreeSystem.Scene_Boot_onDatabaseLoaded.call(this);
    this.process_PurpsMZ_TreeSystem_Notetags();
    console.log("In Scene_Boot");
},

//             process_PurpsMZ_TreeSystem_Notetags
Scene_Boot.prototype.process_PurpsMZ_TreeSystem_Notetags = function(){
    this.process_PurpsMZ_TreeSystem_Data();
    this.process_PurpsMZ_TreeSystem_SkillRankRegExp();
    this.process_PurpsMZ_TreeSystem_Actor_TreeRegExp();
    this.process_PurpsMZ_TreeSystem_Class_TreeRegExp();
},

Spoiler: Game_Actor.setup
Code:
PurpsMZ.TreeSystem.Game_Actor_setup = Game_Actor.prototype.setup;

Game_Actor.prototype.setup = function(actorId) {
    console.log("Setting up Actor: " + actorId);
    PurpsMZ.TreeSystem.Game_Actor_setup.call(this, actorId);
    this.initSp();
    this.initSkillTrees();
};

Game_Actor.prototype.initSp = function() {
    const  actor = this.actor();
    for (let i = 0; i < $dataClasses.length; i++) {
        this.setSp(0,i);
        this.setSpGained(0,i);
    }
};

Game_Actor.prototype.initSkillTrees = function() {
    this._activeTrees = [];
    this._resetTimes = [];
    this._dormantTrees = [];
    this._spentSp = [];
    this._dormantSpentSp = [];
    let skillTrees,
        classId;
  
    if (Imported.VisuMZ_2_ClassChangeSystem && this.multiclasses().length > 1){
        const classes = this.multiclasses();
        for (let idx = 1; idx <= classes.length; ++idx){
            classId = this.multiclassId(idx);
            skillTrees = $dataClasses[classId].skillTrees; 
            if (!skillTrees) continue;
            for(const tree of skillTrees){
                //console.log(classId);
                this.addSkillTree(tree);
            }
        }
    } else{
        classId = this.currentClass().id;
        skillTrees = $dataClasses[classId].skillTrees;
        for(const tree of skillTrees){
            //console.log(classId);
            this.addSkillTree(tree);
        }
    }
      
    skillTrees = $dataActors[this.actorId()].skillTrees;
    for(const tree of skillTrees){
        //console.log(this.actorId());
        this.addSkillTree(tree);
    }
  
    if (Object.keys(this._activeTrees).length > 0 && this._level > 1){
        //Debug - Actor is higher than 1 and has a skill tree.
        console.log("Gaining SP from Starting Level...");
        let diff = this._level;
        while (diff--){
            this.gainSkillPoints();
        }
    }
};

Game_Actor.prototype.addSkillTree = function(treeId) {
    const tree = treeId;
    const Key = this.checkTree(tree);
    if (!Key){
        console.log("This tree doesn't exist.");
        return;
    }
    console.log(this._activeTrees);
    if (this._dormantTrees[Key] != undefined){
        console.log("Has used this Tree before");
        // console.log(this._dormantTrees[treeId]);
        this._activeTrees[Key] = this._dormantTrees[Key];
        this._spentSp[Key] = this._dormantSpentSp[Key];
      
    }else{
        //console.log(this.name() + "'s adding Skill Tree: " + tree['Key']);
        this._activeTrees[Key] = clone(tree);
        this._spentSp[Key] = [];
        this._spentSp[Key]['spTotal'] = 0;
        for(const node of this._activeTrees[Key].Nodes){
            node['Key'] = node['Key'].toLowerCase().trim();
            const skillId = node['Id'];
            if(!skillId){
                continue;
            }
            if (this.isLearnedSkill(skillId)){
                node['rank'] = 1;
            }else{
                node['rank'] = 0;
            }
            this._spentSp[Key][node['Key']] = 0;
        }
        this._resetTimes[Key] = 0;
        //console.log(this.name() + "'s Trees: ");
        //console.log(this._activeTrees[Key]);
    }
    //console.log(this._activeTrees[Key]);
};

Spoiler: Global Function: clone
Code:
const clone = function(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
    }
    return copy;
}

Spoiler: Console Log
New Game:
1609313836127.png


Continue:

1609313871906.png

Any help would be great! I am using the VisuMZ sample project as a base, including this plugin, to test things out, but not sure why it is not loading the trees. If it's working on a new game, shouldn't it work on a continued save?

Look forward to the help!
Database stuff doesn't get loaded to save files.

I can't really follow what you're doing there (due to you only providing fragments of your plugin).

Are you setting up stuff on the database ($dataActors) or on the new game ($gameActors)? The latter will be saved with the save file. The former will not.
Thank you for the quick response! Much appreciated.

Apologies was only trying to provide relevant information for the topic instead of spamming the entirety of my code to shift through!

So the notetag functions are using a new object, $dataTrees that holds the trees from the plugin and pushing the trees from this database on $dataClasses, and $dataActors. Then, what I thought I was doing, was using Scene_Boot to push that information into $gameActors (new game), using the Game_Actor.setup function.

That does help me, to understand that the database doesn't load information from saves, however, Scene_Boot is run when you run the game, so it should work in theory, right?

If you need any more information I'd be happy to provide!

Also the console log calling the "$gameActors.actor(1)._activeTrees" is when I get on new map to test if the trees were saved. The new game it does, which I promptly save the game, and when I reload the save, the second test is showing empty.
In this function:
this.process_PurpsMZ_TreeSystem_Actor_TreeRegExp();

are you updating $dataActors or $gameActors?

I'm not sure, tbh. You're updating Game_Actor.setup which I believe is the proper place. If your save file had been created prior to adding (or changing) your plugin, it wouldn't have this data - could you be trying to load an older save file where it wasn't populated? What other plugins do you have?
In this function:
this.process_PurpsMZ_TreeSystem_Actor_TreeRegExp();

are you updating $dataActors or $gameActors?

I'm not sure, tbh. You're updating Game_Actor.setup which I believe is the proper place. If your save file had been created prior to adding (or changing) your plugin, it wouldn't have this data - could you be trying to load an older save file where it wasn't populated? What other plugins do you have?

To answer this question, I'm using $dataActors, to load the notetags from the database initially.
and the same for $dataClasses for this function:

this.process_PurpsMZ_TreeSystem_Class_TreeRegExp();

I then call Game_Actor.setup to get that information and push it to $gameActors.

I do come from RMMV initially, and this was the way to implement changes to $gameActors, so I am using this plugin as a way to understand the nuance between the two.

In regards to the save-file, I do start with a new game, check the trees from my plugin and then save, overwriting my test save, load the game from the save, and check the trees, and confirm it is empty. This is to make sure I understand why it doesn't load so I know for sure something is still wrong.

I think I got the concept right, but something is goofy with my implementation somehow.

Spoiler: Plugin List
I'm using the VisuMZ Sample Project, and also tried with just my plugin in a fresh project, to the same result.

VisuMZ_0_CoreEngine
VisuMZ_1_BattleCore
VisuMZ_1_ElementStatusCore
VisuMZ_1_EventsMoveCore
VisuMZ_1_ItemsEquipsCore
VisuMZ_1_MainMenuCore
VisuMZ_1_MessageCore
VisuMZ_1_OptionsCore
VisuMZ_1_SaveCore
VisuMZ_1_SkillsStatesCore
PurpsMZ_TreeSystem (My Plugin)
I do come from RMMV initially, and this was the way to implement changes to $gameActors, so I am using this plugin as a way to understand the nuance between the two.
The difference is, $dataActors is a representation of the json file from your game project, so modifying it would affect all the savestats. And as soon as you restart the game or any other event, $dataActors will be reloaded from disk so your changes will be lost.

$gameActors is a representation of the let's say common Actor object. So while $dataActors will bring you data like maxHP, maxMP.. $gameActors will get you info of your Actors right now, e.g. current HP, current Level etc.

If you need to save / load data from a savestat you can use this one
Code:
const aliasMakeSaveContents      = DataManager.makeSaveContents;
const aliasExtractSaveContents   = DataManager.extractSaveContents;

DataManager.makeSaveContents = function() {
    const contents = aliasMakeSaveContents.call(this);
    // your actions here
    return contents;
}

DataManager.extractSaveContents = function(contents) {
    aliasExtractSaveContents.call(this, contents);
    // your actions here
}
The difference is, $dataActors is a representation of the json file from your game project, so modifying it would affect all the savestats. And as soon as you restart the game or any other event, $dataActors will be reloaded from disk so your changes will be lost.

$gameActors is a representation of the let's say common Actor object. So while $dataActors will bring you data like maxHP, maxMP.. $gameActors will get you info of your Actors right now, e.g. current HP, current Level etc.

If you need to save / load data from a savestat you can use this one
Code:
const aliasMakeSaveContents      = DataManager.makeSaveContents;
const aliasExtractSaveContents   = DataManager.extractSaveContents;

DataManager.makeSaveContents = function() {
    const contents = aliasMakeSaveContents.call(this);
    // your actions here
    return contents;
}

DataManager.extractSaveContents = function(contents) {
    aliasExtractSaveContents.call(this, contents);
    // your actions here
}

Oh wow that totally makes sense. Starting from a clean slate would affect things for sure.

I guess a follow up question to that is, why does starting from a new game, which loads the data into $gameActors correctly, (I can test this by calling $gameActors.actor(1)._activeTrees, which is the array that holds all the trees of that actor, from the console and it loads all the right information. In this case, 4 skill trees, Fighter, Knight, Warrior, and Tempus) and then saving the game, loading it back up, it doesn't load the trees at all? I thought since it has been saved into $gameActors, shouldn't it pull the contents just fine, without the need to manually pull the contents?

That's the confusing part for me at least.

EDIT: I went ahead and added those aliases to my plugin for testing purposes and here's my console log data from saving the game (autosave first, then a manual save, then a load)

Spoiler: Console Log & Code
ConsoleSave.JPG

Code:
const aliasMakeSaveContents      = DataManager.makeSaveContents;
const aliasExtractSaveContents   = DataManager.extractSaveContents;

DataManager.makeSaveContents = function() {
    const contents = aliasMakeSaveContents.call(this);
    console.log("Saving game...");
    console.log("Name: " + contents.actors['_data'][1].name());
    console.log(contents.actors['_data'][1]._activeTrees);
    return contents;
}

DataManager.extractSaveContents = function(contents) {
    aliasExtractSaveContents.call(this, contents);
    // your actions here
    console.log("Loading game...");
    console.log("Name: " + contents.actors['_data'][1].name());
    console.log(contents.actors['_data'][1]._activeTrees);
}
I think it might be easier to troubleshoot if you provide your entire plugin so we can take a good look.

I wonder if you need to make a deep copy or stringify your tree object as part of the saving process.
The save process should automatically "deep-stringify" all save data. However, only primitives and basic objects/arrays/prototype structures stringify nicely; a Map, for example, does not. I wonder if you've used something like that in your data structure?

Otherwise, yea, it might help to have the full plugin~
Hey!

Sorry for the delay on this one, with the holidays and work I didn't have time to respond, however if you want to look at my plugin, I don't have a scene or windows yet, as I want to get the core stuff down before I include that, the plugin is attached! 🙂

At the moment, I made it so it requires the VisuMZ Sample Project, so I recommend to put it after the base plugins!

Spoiler: Plugin List
VisuMZ_0_CoreEngine
VisuMZ_1_BattleCore
VisuMZ_1_ElementStatusCore
VisuMZ_1_EventsMoveCore
VisuMZ_1_ItemsEquipsCore
VisuMZ_1_MainMenuCore
VisuMZ_1_MessageCore
VisuMZ_1_OptionsCore
VisuMZ_1_SaveCore
VisuMZ_1_SkillsStatesCore
PurpsMZ_TreeSystem (My Plugin)

Thanks again for the help!

EDIT: Also in order to add the skill trees to actors or classes, feel free to put:
Spoiler: Notetag Information
Code:
<Skill Trees>
Tutorial
</Skill Trees>

In the Note section of Actors or Classes respectfully, I put it on Reid for testing purposes and showing that is saves the data but doesn't load.

Saving:
1609505141299.png


Loading:
1609505208879.png

EDIT 2: Nevermind! I figured it out, it was because I was saving by Key (String) instead of an index (Integer) Thanks for the help! 🙂

Once i changed the entries to an index it loaded fine! 😀

Original thread: https://forums.rpgmakerweb.com/threads/my-plugins-data-is-not-loading-from-a-save.131594/