config.rpgsave - how to store data

Status
Not open for further replies.

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,708
Reaction score
1,127
First Language
Portuguese - Br
Primarily Uses
RMMZ
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? :kaoswt2:
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,113
Reaction score
1,524
First Language
EN
Primarily Uses
RMMZ
  • ConfigManager.makeData creates an object to be saved to the file. It is called by 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. :kaoswt:

  • ConfigManager.applyData does the reverse: it sets the ConfigManager properties to the values retrieved from file. It is called by 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". :kaopride:

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

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,708
Reaction score
1,127
First Language
Portuguese - Br
Primarily Uses
RMMZ
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!
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,113
Reaction score
1,524
First Language
EN
Primarily Uses
RMMZ
...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. :kaoswt2:

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~ :kaothx:

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:
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.
JavaScript:
Object.defineProperties(Game_BattlerBase.prototype, {
// ...
// Maximum Hit Points
    mhp: { get: function() { return this.param(0); }, configurable: true },
 

hiddenone

Lurker Extraordinaire
Global Mod
Joined
Feb 19, 2014
Messages
2,496
Reaction score
5,332
First Language
english
Primarily Uses
RMMZ

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.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.
time for a new avatar :)

Forum statistics

Threads
106,017
Messages
1,018,356
Members
137,802
Latest member
rencarbali
Top