config.rpgsave - how to store data

Hi people!
I'm trying to understand how can I store custom data inside this file.
The reason is that I want a global object to be saved for different save files.
Well, somehow, I managed to do that!
I change the global object in-game and call ConfigManager.save(), then my global object is saved across different save files.

BUT!
The problem is, I can't understand how I do it and why it's working. I've tried to replicate the same pattern that Rpg Maker Mv code does to save the bgmVolume. And somehow managed to work. But I can't really understand why =/

Here is what I have:
JavaScript:
ConfigManager.casa = {
    Stage0: null,
    Stage1: [],
    Stage2: [],
    Stage3: [],
    Stage4: [],
    Stage5: [],
    Stage6: []
}

Object.defineProperty(ConfigManager, 'casa', {
    get: function() {
        return casa;
    },
    set: function(value) {
        casa = value;
    },
    configurable: true
});

var _ConfigManager_makeData = ConfigManager.makeData;
ConfigManager.makeData = function() {
    let config = _ConfigManager_makeData.call(this);
    config.casa = this.casa;
    return config;
};

var _ConfigManager_applyData = ConfigManager.applyData;
ConfigManager.applyData = function(config) {
    _ConfigManager_applyData.call(this,config);
    this.casa = this.readCustom(config, 'casa');
};

ConfigManager.readCustom = function(config, name) {
    var value = config[name];
    if (value !== undefined) {
        return config[name];
    } else {
        return {   
            Stage0: null,
            Stage1: [],
            Stage2: [],
            Stage3: [],
            Stage4: [],
            Stage5: [],
            Stage6: []
        };
    }
};

So please, can you try to explain my code to me? 😅
  • ConfigManager.makeData creates an object to be saved to the file. It is called by ConfigManager.save:
    Spoiler: ConfigManager.save
    JavaScript:
    // rpg_managers.js
    ConfigManager.save = function() {
        StorageManager.save(-1, JSON.stringify(this.makeData()));
    };
    I.e. "save the makeData result in JSON format to slot -1 (config)". This is needed because you don't want to save all the code associated with the ConfigManager; you only want to save the properties that change. 😅

  • ConfigManager.applyData does the reverse: it sets the ConfigManager properties to the values retrieved from file. It is called by ConfigManager.load:
    Spoiler: ConfigManager.load
    JavaScript:
    // rpg_managers.js
    ConfigManager.load = function() {
        var json;
        var config = {};
        try {
            json = StorageManager.load(-1);
        } catch (e) {
            console.error(e);
        }
        if (json) {
            config = JSON.parse(json);
        }
        this.applyData(config);
    };
    I.e. "load save slot -1 (config), parse that data (string -> object), then apply these values to the ConfigManager". 😤

  • defineProperty just formally defines a property on an object. In case you're interested, you can read up on it here:
Thanks @caethyril ! ^^

I think I'm getting it...
So in fact, because of this:
Code:
ConfigManager.readCustom = function(config, name) {
    var value = config[name];
    if (value !== undefined) {
        return config[name];
    } else {
        return {   
            Stage0: null,
            Stage1: [],
            Stage2: [],
            Stage3: [],
            Stage4: [],
            Stage5: [],
            Stage6: []
        };
    }
};

I don't need to do this:
Code:
ConfigManager.casa = {
    Stage0: null,
    Stage1: [],
    Stage2: [],
    Stage3: [],
    Stage4: [],
    Stage5: [],
    Stage6: []
}

because if ConfigManager.casa is undefined, the ConfigManager.readCustom will set a default value to it. Right?

Reading about the Object.defineProperty...
" just formally defines a property on an object."
So it seems that this can be removed from the code since I can add a new property and a new value to any property like this:
casa.heigth = 100;
?

I guess I will have to learn more about this.
I can't understand why has this "Object.defineProperty", if I can add property and values like above xD
So I guess I'm missing something. But I will keep reading!
...if ConfigManager.casa is undefined, the ConfigManager.readCustom will set a default value to it. Right?
Yes; in that case ConfigManager.casa will be undefined until it loads the settings from file, which happens when Scene_Boot starts. That should be OK, unless you need it to process stuff as soon as the game loads for some reason. 😅

So it seems that this can be removed from the code since I can add a new property and a new value to any property like this:
casa.heigth = 100;
?

I can't understand why has this "Object.defineProperty", if I can add property and values like above xD
So I guess I'm missing something. But I will keep reading!
Yes, in your case it's unnecessary~ 🙏

Among other things, Object.defineProperty lets you define properties with custom get/set methods. You can make read-only properties, or properties that act as a kind of alias for getting values from elsewhere. For example, ConfigManager.bgmVolume (rpg_managers.js) just points directly to the AudioManager:
Spoiler: ConfigManager.bgmVolume
JavaScript:
Object.defineProperty(ConfigManager, 'bgmVolume', {
    get: function() {
        return AudioManager._bgmVolume;
    },
    set: function(value) {
        AudioManager.bgmVolume = value;
    },
    configurable: true
});
Another example is Game_BattlerBase.prototype.mhp (rpg_objects.js): it has a custom "get" method (a.k.a. "getter") so that whenever you ask for a battler's mhp it calls the appropriate function to calculate it, including traits etc.
Spoiler: Game_Battler.prototype.mhp
JavaScript:
Object.defineProperties(Game_BattlerBase.prototype, {
// ...
// Maximum Hit Points
    mhp: { get: function() { return this.param(0); }, configurable: true },
Thank you @caethyril !
I think I understand that now!
:MV1:

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/config-rpgsave-how-to-store-data.119099/