Object loses functions after save is loaded

Hey everyone, I have an issue that seems strange to me but might just be normal. I am nearly done with this plugin I am working on and I am testing the saving and loading of the object I have created to store data for this plugin into the save file. Everything is working fine, the data is saving to the save file and loading fine as well except for one issue.

When I examine the global var in the console, under the Object prototype drop down all of the functions I defined for the object are gone and replaced with generic looking junk. Here is what I mean below with images because I can't figure a way to express the issue properly:

The object functions during a new game:
new_game.png



The object functions after a save has been loaded:
after_load.png

The end result is when I go to call the function I have set a watch variable for, the game crashes with a $gameBanks.bankExists is not a function error message.

Anyone have any ideas? I have looked at how other such global vars like $gameParty are loaded and saved and I cannot find where there is a different to how I have added in my global var to the rpg_managers.js file. If you need more code like the Game_Banks definition or the stuff I added to rpg_managers please let me know.
Functions are not saved on the file, instead, the game should understand that the object being loaded is an instance of a class (Game_Banks) . You need to share your class definition so we can try to find the problem.
Very well, here is everything then for the class def and the few changes I made in rpg_managers:

Game_Banks:
Code:
    /************
    * Game_Banks
    * Class to hold all bank information for plugin
    *************
    */

    function Game_Banks(){
        this.initialize.apply(this,arguments);
    };

    Object.defineProperties(Game_Banks.prototype,{
        banks: {get: function(){return this._banks;}, configurable: true}
    });


    Game_Banks.prototype.initialize = function(){
        this.initMembers();
    };

    Game_Banks.prototype.addBank = function(bID, bBalance, bInterest, bLastAccess){
        if (!this._banks[bID]){
            this._banks[bID] = {};

            this._banks[bID]["balance"] = bBalance;
            this._banks[bID]["interest"] = bInterest;
            this._banks[bID]["lastAccess"]    = bLastAccess;
            this._banks[bID]["bonds"] = {};
            this._banks[bID]["bondInfo"] = {};
            this._banks[bID]["stocks"] = {};
        }
    };

    Game_Banks.prototype.initMembers = function(){
        this._banks = [];
    };

    Game_Banks.prototype.setBalance = function(bID, bBalance){
        this._banks[bID]["balance"] = bBalance;
    };

    Game_Banks.prototype.setInterest = function(bID, bInt){
        this._banks[bID]["interest"] = bInt;
    };

    Game_Banks.prototype.setLastAccess = function(bID, bLAccess){
        this._banks[bID]["lastAccess"] = bLAccess;
    };

    Game_Banks.prototype.getBalance = function(bID){
        return this._banks[bID]["balance"];
    };

    Game_Banks.prototype.getInterest = function(bID){
        return this._banks[bID]["interest"];
    };

    Game_Banks.prototype.getLastAccess = function(bID){
        return this._banks[bID]["lastAccess"];
    };

    Game_Banks.prototype.bankExists = function(bID){
        return !this._banks[bID] ? false:true;
    };

    Game_Banks.prototype.getBank = function(bID){
        return this._banks[bID];
    };

    Game_Banks.prototype.updateBank = function(bID,bank){
        this._banks[bID] = bank;
    };

    Game_Banks.prototype.addBond = function(bID,bndID,bndName,bndCost,bndMVal,bndMTime,bndPRDate){
        var curBank = this.getBank(bID);
        curBank["bonds"][bndID] = {};

        curBank["bonds"][bndID]["id"] = bndID;
        curBank["bonds"][bndID]["name"] = bndName;
        curBank["bonds"][bndID]["cost"] = bndCost;
        curBank["bonds"][bndID]["maturedValue"] = bndMVal;
        curBank["bonds"][bndID]["matureTime"] = bndMTime;
        curBank["bonds"][bndID]["purchaseDate"] = bndPRDate;
    };

    Game_Banks.prototype.addBondInfo = function(bnkID,biID,bndName,bndCost,bndMVal,bndMTime,bndHlpTxt){
        var curBank = this.getBank(bnkID);
        curBank["bondInfo"][biID] = {};

        bndHlpTxt = bndHlpTxt.replace("\\n","\n")

        curBank["bondInfo"][biID]["id"] = biID;
        curBank["bondInfo"][biID]["name"] = bndName;
        curBank["bondInfo"][biID]["cost"] = bndCost;
        curBank["bondInfo"][biID]["maturedValue"] = bndMVal;
        curBank["bondInfo"][biID]["matureTime"] = bndMTime;
        curBank["bondInfo"][biID]["helpText"] = bndHlpTxt;
    };

    Game_Banks.prototype.bDoBondsExist = function(bnkId){
        return this._banks[bnkId]["bondInfo"].length > 0 ? true : false;
    }

    Game_Banks.prototype.bDoesBondExist = function(bnkId,biId){
        return this._banks[bnkId]["bondInfo"][biId] != null ? true : false;
    };

    Game_Banks.prototype.getBond = function(bnkID,bID){
        return this._banks[bnkID]["bonds"][bID];
    };

    Game_Banks.prototype.removeBond = function(bnkID,bnd){
        delete this._banks[bnkID]["bonds"][bnd["id"]];
    }

rpg_managers edits:
Code:
var $dataActors       = null;
var $dataClasses      = null;
var $dataSkills       = null;
var $dataItems        = null;
var $dataWeapons      = null;
var $dataArmors       = null;
var $dataEnemies      = null;
var $dataTroops       = null;
var $dataStates       = null;
var $dataAnimations   = null;
var $dataTilesets     = null;
var $dataCommonEvents = null;
var $dataSystem       = null;
var $dataMapInfos     = null;
var $dataMap          = null;
var $gameTemp         = null;
var $gameSystem       = null;
var $gameScreen       = null;
var $gameTimer        = null;
var $gameMessage      = null;
var $gameSwitches     = null;
var $gameVariables    = null;
var $gameSelfSwitches = null;
var $gameActors       = null;
var $gameParty        = null;
var $gameTroop        = null;
var $gameMap          = null;
var $gamePlayer       = null;
var $testEvent        = null;
var $gameBanks        = null; //For Geowil_BankPlugin

DataManager.makeSaveContents = function() {
    // A save data does not contain $gameTemp, $gameMessage, and $gameTroop.
    var contents = {};
    contents.system       = $gameSystem;
    contents.screen       = $gameScreen;
    contents.timer        = $gameTimer;
    contents.switches     = $gameSwitches;
    contents.variables    = $gameVariables;
    contents.selfSwitches = $gameSelfSwitches;
    contents.actors       = $gameActors;
    contents.party        = $gameParty;
    contents.map          = $gameMap;
    contents.player       = $gamePlayer;
    contents.banks        = $gameBanks; //For GeoWil_BankPlugin
    return contents;
};

DataManager.extractSaveContents = function(contents) {
    $gameSystem        = contents.system;
    $gameScreen        = contents.screen;
    $gameTimer         = contents.timer;
    $gameSwitches      = contents.switches;
    $gameVariables     = contents.variables;
    $gameSelfSwitches  = contents.selfSwitches;
    $gameActors        = contents.actors;
    $gameParty         = contents.party;
    $gameMap           = contents.map;
    $gamePlayer        = contents.player;
    $gameBanks         = contents.banks; //For GeoWil_BankPlugin
};

After digging around in the debugger while the contents json is being loaded and parsed it seems like the class definition in the save file is either being removed or ignored as once it comes out of the json parse contents.banks is no longer a Game_Banks object, its just an Object.
hi, when you load game or new game, rmmv setupNewGame and initialise all again.
You need hack to also initialise all your function again.
gameEngine Scene WorkFlow.png
Your Game_Bank class lacks a constructor function. Therefore when you save an object to the json file, when it gets loaded back, the JS interpreter doesn't know anymore which class it's been a part of.

That's why the first three lines should rather be like this:

Code:
function Game_Bank () {
this.initialize.apply (this, arguments);
}

Game_Bank.prototype = Object.create(Object.prototype); //Maybe an extra line that doesn't need to be there
Game_Bank.prototype.constructor = Game_Bank;

Now the JS interpreter will know that the object has a constructor function and therefore will remember what kind of an object it is once you load it.
Your Game_Bank class lacks a constructor function. Therefore when you save an object to the json file, when it gets loaded back, the JS interpreter doesn't know anymore which class it's been a part of.

That's why the first three lines should rather be like this:

Code:
function Game_Bank () {
this.initialize.apply (this, arguments);
}

Game_Bank.prototype = Object.create(Object.prototype); //Maybe an extra line that doesn't need to be there
Game_Bank.prototype.constructor = Game_Bank;

Now the JS interpreter will know that the object has a constructor function and therefore will remember what kind of an object it is once you load it.

After inserting the constructor as you suggested I am still getting the same error and the $gameBanks class type is still Object instead of Game_Banks when opening the bank scene after loading a new save game.
Damn, I got it working. I had to move the Game_Banks definition from my plugin js file to rpg_objects.js and the test bank scene started working on loading.

Thanks for the help though, I am sure that would have still been an issue.
It's weird. But good to know that you found the answer 🙂

Original thread: https://forums.rpgmakerweb.com/threads/object-loses-functions-after-save-is-loaded.97018/