JS Variables resetting after reloading the game?

I have an event that calls Common Events which have scripts to generate arrays. (Most of them use the $whatever format.) If I save the game after they're created and try to reload, it appears they have reset and I get the undefined error message. Is there an easy way to ensure any custom variables I define using the script command will remain permanent?

For example, I'd script:
$examplearray = ["something", "okay"]

I just learned the bare essentials of JS, so I'm not sure if I'm doing something wrong, or if this is normal behavior when interacting with RMMV.
Thanks in advance for any help.
You need to make sure they get saved when the rest of the game is saved.  You do this by putting them into a global variable (like the $gameXYZ variables) and modifying the save/load functions to include your global variable as well, or you put them INTO one of the existing global variables, like $gameSystem, $gamePlayer, $gameParty - wherever it seems most appropriate.
You need to make sure they get saved when the rest of the game is saved.  You do this by putting them into a global variable (like the $gameXYZ variables) and modifying the save/load functions to include your global variable as well, or you put them INTO one of the existing global variables, like $gameSystem, $gamePlayer, $gameParty - wherever it seems most appropriate.

Now you said that?!!


It took me long time to make new variables and make it include with save/load.


you are the best bro 😛
Sorry to interfere, but I too am interested in this topic.

Now you said that?!!


It took me long time to make new variables and make it include with save/load.


you are the best bro 😛

I'm trying to do the same thing without much avail. Could you better explain how you managed to do it, pretty please?


(Or you could post your edited save/load functions, so I'll try to understand by myself)
Sorry to interfere, but I too am interested in this topic.


I'm trying to do the same thing without much avail. Could you better explain how you managed to do it, pretty please?


(Or you could post your edited save/load functions, so I'll try to understand by myself)

I think the way shaz suggest is better, but if you insist i will upload it for you 😉 .


I was making maps so i can teleport so it will be a little confusing .


i hope u can figure out.

View attachment MoreVariables&Switches.js
You need to make sure they get saved when the rest of the game is saved.  You do this by putting them into a global variable (like the $gameXYZ variables) and modifying the save/load functions to include your global variable as well, or you put them INTO one of the existing global variables, like $gameSystem, $gamePlayer, $gameParty - wherever it seems most appropriate.


Thanks so much! I think this will work just fine. I'll explain what I did in case there are others with my problem who, like me, barely understand what they are doing when it comes to RMMV/JS scripting. It seems like a sloppy fix the way I did it, so there's probably a more optimal way if you researched it more to understand coding and all of that.

I had a map event that called Common Events to 'initialize' my custom databases. They defined arrays (example: $thing1 = ["this", "is", 1, "thing"]), and there were a bunch. I had 400+ arrays of 10 elements each. When I had other common events that sliced these to generate additional arrays, it started to make the code take a long time to run. I tried to just save the game after initializing so that I wouldn't have to wait that long just to play-test little changes that used these arrays. They weren't saved after the game re-loaded, so I came here for help.

So, based on what Shaz said, I figured I go ahead and integrate them into the game's main global variables that are handled with the save and load functions. I went into "rpg_managers.js" (located in your ProjectName/js/ folder) and edited it to include my custom variable. Right at the top, under DataManager, there are a list of globally defined variables ($dataActors, $dataClasses, etc). I created a new line at the bottom of this list with " var $mycustomvariable = ["some", "stuff"]; ". This worked just fine, and it was saved along with everything else when I reloaded. Again, I can't recommend messing around with the game's core JS files, as I don't know what I'm doing. But this is a quick fix that appears to solve my problem. I'm not using much of the core game engine itself, just using the application because I'm more familiar with the Rpg Maker series than any particular scripting language or coding engine. For all I know, adding too many global variables to that list might slow down your game save/loading process, so beware my advice.

Thanks again for the help, and I hope my explanation helps clarify my problem-solving process.
You're not doing what I recommended, so while it may be working for you, I don't think it's doing what YOU think it's doing.


$dataXYZ classes are created on loading the game, prior to getting to the title screen.  They're only loaded once.  This is NOT save data, and has nothing to do with games in progress or save files.  This data does not need to be saved when the player's game is saved.


My reference to $gamePlayer, $gameSystem, $gameParty (notice they are $game, not $data) was for variables that change as you play, and could be different from one save file to the next.  In THOSE cases, you would need to modify the save/load functions to include them.


I might have misunderstood and the first one is what you wanted all along.

Original thread: https://forums.rpgmakerweb.com/threads/js-variables-resetting-after-reloading-the-game.67815/