GameSystem objects not saving in save file

Status
Not open for further replies.

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,710
Reaction score
1,127
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi people!

I have some problems with my plugin. I have a global object, that is a parameter from the plugin:
Eli.HelpWindows.Param.menuHelp.text > This return to me various objects with string values.

I have an option that can change this string values via script calls or plugin commands. I was using this: Eli.HelpWindows.Param.menuHelp.text["object"] = "anything".

But this saves globally and if another new Game is chosen, the value will be from the other game.

So to avoid this, I alias the Game_System.prototype.initialize and create a new object that carries the parameters of the plugin:

JavaScript:
Eli.HelpWindows.Game_System_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function() {
Eli.HelpWindows.Game_System_initialize.call(this);
this._menuHelp.text = Eli.HelpWindows.Param.menuHelp.text;
};
Then, instead of using the value from Eli.HelpWindows.Param.menuHelp.text I used the $gameSystem._menuHelp.text to set and change the values.

In my tests, I start a new game and change the value via script call:
JavaScript:
$gameSystem._menuHelp.text["object"] = "Hello world";
Then I save the game via menu command.

When I go to the title(via menu>game end) and start a new game, the value remains the one that I have changed via script call in the other save slot.

BUT!

If I go to game Title pressing F5 and start a new game, the value returns to its default. And in the save file, the value is as changed in the script call.

I make the same test with a game variable value, and when I start a new game it is immediately and automatically set to 0.

I take a look at the Rpg_managers and I see that the game objects are created in DataManager.setupNewGame() that I think that is called in Start New game command.

The DataManager.setupNewGame() create both Game_System Objects and Game_Variables objects. So why is the Game_System the value is not reset automatically at a New Game Start and in Game_Variables, the value is reset?

JavaScript:
DataManager.createGameObjects = function() {
    $gameTemp          = new Game_Temp();
    $gameSystem        = new Game_System();
    $gameScreen        = new Game_Screen();
    $gameTimer         = new Game_Timer();
    $gameMessage       = new Game_Message();
    $gameSwitches      = new Game_Switches();
    $gameVariables     = new Game_Variables();
    $gameSelfSwitches  = new Game_SelfSwitches();
    $gameActors        = new Game_Actors();
    $gameParty         = new Game_Party();
    $gameTroop         = new Game_Troop();
    $gameMap           = new Game_Map();
    $gamePlayer        = new Game_Player();
};

DataManager.setupNewGame = function() {
    this.createGameObjects();
    this.selectSavefileForNewGame();
    $gameParty.setupStartingMembers();
    $gamePlayer.reserveTransfer($dataSystem.startMapId,
        $dataSystem.startX, $dataSystem.startY);
    Graphics.frameCount = 0;

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;
    return contents;
};

};

What I'm missing?

[EDIT] I have tried to use:
this._menuHelp.text = Eli.HelpWindows.Param.menuHelp.text;
this._menuHelp.text = {...Eli.HelpWindows.Param.menuHelp.text};
this._menuHelp.text = Object.assign({}, Eli.HelpWindows.Param.menuHelp.text};


But no work.
The only method that is working is this one:
this._menuHelp.text = JSON.parse(JSON.stringify(Eli.HelpWindows.Param.menuHelp))(made a deep copy).

Now it works properly. But I still don't get it why Mv behave like that. o_O
 
Last edited:

Silva

Scoobityboo
Veteran
Joined
Nov 5, 2018
Messages
399
Reaction score
221
First Language
English
Primarily Uses
RMMV
This is normal behaviour in js when dealing with objects. When you do

JavaScript:
this._menuHelp.text = Eli.HelpWindows.Param.menuHelp.text;
and then later use "this._menuHelp.text", you're actually using "Eli.HelpWindows.Param.menuHelp.text". It effectively treats it as a shortcut, rather than cloning the values and creating a new object. It's for this reason that the engine can do things like this:

JavaScript:
Game_Action.prototype.apply = function(target) {
    var result = target.result();
    this.subject().clearResult();
    result.clear();
    result.used = this.testApply(target);
    result.missed = (result.used && Math.random() >= this.itemHit(target));
    //and so on so forth
    }
};
And then later continue to use the result method on the battler to get the modified values. If target.result() was being cloned at this stage the changes would not update on the battler and would only be available in the functions scope.

Re use of the spread operator and assign - I'm not sure why these wouldn't work because as far as I'm aware they're both capable of making shallow copies of an object, which should be fine provided the object your cloning doesn't contain any objects.
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,710
Reaction score
1,127
First Language
Portuguese - Br
Primarily Uses
RMMZ
Re use of the spread operator and assign - I'm not sure why these wouldn't work because as far as I'm aware they're both capable of making shallow copies of an object, which should be fine provided the object your cloning doesn't contain any objects.
@Silva Ohh! So that's it! My object does contain other objects inside... I have read this, but not understand it correctly. Now that I read your comment I get it.
Thanks!
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,381
Reaction score
8,537
First Language
English
Primarily Uses
RMMV

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 Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,048
Messages
1,018,543
Members
137,834
Latest member
EverNoir
Top