Map/Hashmap/Hashtable datatype erased after Save/Load

Hi, I've got a question specific to some of the more basic Javascript functionality that RPGM uses.

I've been using Map/Hashtable objects to store information about the game. Particularly things that the players have unlocked. I store them into $gameVariables objects like everything else, so for instance I make sure that at the start of the game it initializes a set of discoverable things in the game as $gameVariables.setValue(xx, new Map()); or I'll use the drag and drop code to set it to a new Map(), then I'll populate it. Checking the console, everything works out; the keys and value pairs show up in the exact order I put them in. The maps function exactly as intended, just like other Array variables that I use for various other things. But after a few months of using this system without a hitch, I started running into problems when saving the game and then reloading it, which is something I sparsely do because debugging RPGs as a whole takes a long time.

I tested a few different cases with other variables where it would store and load Arrays, for instance, perfectly fine. But the Map objects I store--the ones that work perfectly fine if I keep the game running--are reset to empty Objects. Like the Map could contain several key-value pairs and its type is a Map object but once I reload, it's turned into an Object type object with nothing in it instead. Maps are probably the most exotic data type I use for this project, but they're not exactly sophisticated. Is there something I'm doing wrong here, or are Maps just forbidden from RPGM and they owe me like two months of labor?
Hello! When the game save, the main game Objects are turned into string and saved as JSON.
This will save the object properties, but not the methods (JSON doesn't allow functions as object values).
It would be better to build your custom methods in a plugin file and store the information that you want to be retained upon save game as a property of Game_System.
Details:
RMMV implements an extension to this, JsonEx, which has handlers for circular references and prototyped objects.
Spoiler: Excerpt: JsonEx._encode (rpg_core.js)
JavaScript:
if (type === '[object Object]' || type === '[object Array]') {
        value['@c'] = JsonEx._generateId();

        var constructorName = this._getConstructorName(value);
        if (constructorName !== 'Object' && constructorName !== 'Array') {
            value['@'] = constructorName;
        }
The Map type is not handled by this extension, so it will be passed as-is to the default JSON.stringify method, causing it to be skipped. Some possibilities:
  1. Change your plugin to use a stringifiable data type. Recommended! I also imagine that this would be the easiest option, assuming your code obeys the DRY principle ("Don't Repeat Yourself").

  2. Manually convert your data before/after saving. cf DataManager methods makeSaveContents & extractSaveContents.

  3. Extend the default JsonEx methods, _encode and _decode (cf rpg_core.js), to correctly treat the required types. I'm not certain of what would be involved in that. 😴
Hello! When the game save, the main game Objects are turned into string and saved as JSON.
This will save the object properties, but not the methods (JSON doesn't allow functions as object values).
It would be better to build your custom methods in a plugin file and store the information that you want to be retained upon save game as a property of Game_System.

Do you have any recommendations on where best to find tutorials on that? I haven't ever even made a conventional Javascript file. I've strictly just been operating within RPGM as an IDE.


  1. Change your plugin to use a stringifiable data type. Recommended! I also imagine that this would be the easiest option, assuming your code obeys the DRY principle ("Don't Repeat Yourself").

  2. Manually convert your data before/after saving. cf DataManager methods makeSaveContents & extractSaveContents.

  3. Extend the default JsonEx methods, _encode and _decode (cf rpg_core.js), to correctly treat the required types. I'm not certain of what would be involved in that. 😴

I haven't been making my own plugins, though with the amount of Javascript I've written it probably would've saved me a lot of headaches. I don't know how to use the DataManager methods, but I'll look into it. We're sort of on a time crunch, so I might hack something together like writing the hashmap to an array and then loading that stuff back into a hashmap when the game is loaded.

I'll be watching the thread for responses since I still need them, but thanks for what you've contributed so far, having my suspicions confirmed is a big help.
Hello again! If you're asking for a tutorial about how to make a plugin file, I'm sure you'll find a lot on the forum. I cannot indicate you which one is better, though.

Original thread: https://forums.rpgmakerweb.com/threads/map-hashmap-hashtable-datatype-erased-after-save-load.133059/