RMMV Objects within the $gameVariables object not saving. (SOLVED)

flynnmichael81

Veteran
Veteran
Joined
Jun 1, 2014
Messages
40
Reaction score
37
First Language
English
Primarily Uses
N/A
I've been using the $gameVariables object as the base to all my code as an easy way of saving it using the default MV save.

However, when I create objects within that: $gameVariables.map = {} they don't save.

I realize there is an issue with cloning going on here but can anyone suggest a solution to this? Thank you!
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,483
Reaction score
720
First Language
indonesian
I've been using the $gameVariables object as the base to all my code as an easy way of saving it using the default MV save.

However, when I create objects within that: $gameVariables.map = {} they don't save.

I realize there is an issue with cloning going on here but can anyone suggest a solution to this? Thank you!
can you share where you place that code?
Code:
$gameVariables.map = {}

also any error shown in console or not?
 

flynnmichael81

Veteran
Veteran
Joined
Jun 1, 2014
Messages
40
Reaction score
37
First Language
English
Primarily Uses
N/A
Hi, yeah, it's used a lot in my code but here is an example.
Code:
var showMainMap = function() {

  $gameVariables.mouseOFF = false
  makeHelpMain()
  $gameScreen.showPicture(1,"BACK",0,0,0,100,100,255, 0)
 
  drawMainButtons()


  var mmx = 0
  var mmy = 0
  var ix = 90
  var iy = -7
  var iNum = 0
  $gameVariables.qc = 0

  while (mmy<26) {
    while (mmx<26) {

      if ($gameVariables.fow == true) {
        var fog = $gameVariables.map["tile"+iNum].allSupply[$gameVariables.playerDesig]
        if (fog>1||iNum==298||iNum==34||iNum==74||iNum==336||iNum==617||iNum==313||iNum==551) {
            var picture = $gameVariables.map["tile"+iNum].prefix + $gameVariables.map["tile"+iNum].tNum +"" //"" + $gameVariables.mainMap[mmy][mmx] + $gameVariables.mapNums[iNum]
            $gameScreen.showPicture(2700+(iNum),picture,0,ix,iy,15.625,15.625,255,0)
        }else{
           // $gameScreen.erasePicture(2700+(iNum))
        }
      }else{
          var picture = $gameVariables.map["tile"+iNum].prefix + $gameVariables.map["tile"+iNum].tNum +"" //"" + $gameVariables.mainMap[mmy][mmx] + $gameVariables.mapNums[iNum]
            $gameScreen.showPicture(2700+(iNum),picture,0,ix,iy,15.625,15.625,255,0)
       }
      

    updateMapButtons(iNum,ix,iy)

      mmx++
      iNum++
      ix += 40
    }
     mmy++
     mmx = 0
     iy += 40
     ix = 90
  }
}

It all works fine, but it wont save using the default game save. I've tried stringify-ing and parse-ing but it doesn't seem to help.

The error in console says : Cannot read property '1' of undefined - When I type $gameVariables.map into the console it shows that the data hasn't been copied across. It's something to with the game engine only shallow cloning the object, but I don't know how to fix it.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,040
Reaction score
2,297
First Language
English
Primarily Uses
RMMV
Yeah, I believe it only serialises (and retrieves) the _data property, so anything you save will have to also be in there unless you modify the way game saving works. So you'd have to do something like $gameVariables._data["mouseOff"] = false. That should save along with the regular data.
 

flynnmichael81

Veteran
Veteran
Joined
Jun 1, 2014
Messages
40
Reaction score
37
First Language
English
Primarily Uses
N/A
Thanks. I'll just use jQuery and .extend instead of relying on RMMV's JSON.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,414
Reaction score
1,823
First Language
EN
Primarily Uses
RMMZ
I disagree with @Trihan: $gameVariables is accurately deep-stringified prior to saving and deep-parsed after loading. Test case:
  1. Make a new project
  2. Playtest -> New Game
  3. Open the console (F8) and enter: $gameVariables.test = { a: 1, b: 2 };
  4. Menu -> Save
  5. Close the playtest window
  6. Playtest -> Continue
  7. Open the console (F8) and enter: $gameVariables.test
Certain data types (e.g. Functions and Maps) are not stringified, thus will be omitted...helpful for stringifying prototyped objects! I wonder if your problem might actually be a timing issue, though. Note that $gameVariables is initialised at various points:
  • Game boot,
  • New game,
  • Load game, just before applying data from save file
Since you are not hooking your data into the Game_Variables prototype, every initialisation will wipe that data.

Instead I would suggest keeping the data in your own object and manually adding that into the new/save/load routines, e.g.
JavaScript:
var myData = {};  // your plugin namespace

// New game / init
(function(alias) {
  DataManager.createGameObjects = function() {
    alias.apply(this, arguments);
    myData.map = {};
  };
})(DataManager.createGameObjects);

// Save game
(function(alias) {
  DataManager.makeSaveContents = function() {
    var contents = alias.apply(this, arguments);
    contents.map = myData.map;
    return contents;
  };
})(DataManager.createGameObjects);

// Load game
(function(alias) {
  DataManager.makeSaveContents = function(contents) {
    alias.apply(this, arguments);
    myData.map = contents.map;
  };
})(DataManager.extractSaveContents);
This can also help to avoid conflicts with other plugins that might manipulate the existing $gameVariables object for whatever reason. Naturally you can rename myData to something more appropriate, and/or nest it under an author namespace or something~
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,040
Reaction score
2,297
First Language
English
Primarily Uses
RMMV
I disagree with @Trihan: $gameVariables is accurately deep-stringified prior to saving and deep-parsed after loading. Test case:
  1. Make a new project
  2. Playtest -> New Game
  3. Open the console (F8) and enter: $gameVariables.test = { a: 1, b: 2 };
  4. Menu -> Save
  5. Close the playtest window
  6. Playtest -> Continue
  7. Open the console (F8) and enter: $gameVariables.test
Certain data types (e.g. Functions and Maps) are not stringified, thus will be omitted...helpful for stringifying prototyped objects! I wonder if your problem might actually be a timing issue, though. Note that $gameVariables is initialised at various points:
  • Game boot,
  • New game,
  • Load game, just before applying data from save file
Since you are not hooking your data into the Game_Variables prototype, every initialisation will wipe that data.

Instead I would suggest keeping the data in your own object and manually adding that into the new/save/load routines, e.g.
JavaScript:
var myData = {};  // your plugin namespace[/INDENT]
[INDENT][/INDENT]
[INDENT]// New game / init[/INDENT]
[INDENT](function(alias) {[/INDENT]
[INDENT]  DataManager.createGameObjects = function() {[/INDENT]
[INDENT]    alias.apply(this, arguments);[/INDENT]
[INDENT]    myData.map = {};[/INDENT]
[INDENT]  };[/INDENT]
[INDENT]})(DataManager.createGameObjects);[/INDENT]
[INDENT][/INDENT]
[INDENT]// Save game[/INDENT]
[INDENT](function(alias) {[/INDENT]
[INDENT]  DataManager.makeSaveContents = function() {[/INDENT]
[INDENT]    var contents = alias.apply(this, arguments);[/INDENT]
[INDENT]    contents.map = myData.map;[/INDENT]
[INDENT]    return contents;[/INDENT]
[INDENT]  };[/INDENT]
[INDENT]})(DataManager.createGameObjects);[/INDENT]
[INDENT][/INDENT]
[INDENT]// Load game[/INDENT]
[INDENT](function(alias) {[/INDENT]
[INDENT]  DataManager.makeSaveContents = function(contents) {[/INDENT]
[INDENT]    alias.apply(this, arguments);[/INDENT]
[INDENT]    myData.map = contents.map;[/INDENT]
[INDENT]  };[/INDENT]
[INDENT]})(DataManager.extractSaveContents);
This can also help to avoid conflicts with other plugins that might manipulate the existing $gameVariables object for whatever reason. Naturally you can rename myData to something more appropriate, and/or nest it under an author namespace or something~
I stand corrected!
 

Latest Threads

Latest Profile Posts

Tired of just doing mobile. Now you can Play the game in ur Browser! https://bigmala-productions.itch.io/rpg-forever
I managed to fix my car at last... :biggrin:

I'm proud of what I have achieved with my project. Don't mind with the Spanish, Ill add more languages.
Im currently looking at the RPG MAKER Steam page, and i found out the Rpg maker mz requires bit 64 pc, i only have bit 32 pc ;-;, i am really using an ancient pc.. XD

Forum statistics

Threads
111,281
Messages
1,059,868
Members
144,584
Latest member
gillfigno
Top