Storing arrays persistently?

Hello,

I'm implementing a fast travel mechanic in my game. Basically, the player has an item that allows him to store locations. He can then use the item to call up a menu to return to these locations instantly.
The item stores the Map ID, as well as the X and Y coordinates of the player in the moment he stores the location.

Now, the original idea was to use a script to store this information in an array. However, I discovered that variables created in a script are not persistant, meaning as soon as I quit the game, they're gone. So next time the player starts the game, all stored locations are lost.
So I tried to store the data in a game variable. However, while game variables apparently can be arrays, they're weird. None of the array functions seemed to work with them.

For example:
$gameVariables.value(100).forEach(function(element){
arrayMapIndexes.push(element);
});

retuns an "undefined is not a function" error. However, if I do
var testArray = [1,2,3];
testArray.forEach(function(element){
arrayMapIndexes.push(element);
});

this works.

Does anyone have an idea how this can be done? Is there any way to store an array persistently in RPG Maker?
Simple. Game variables aren't arrays by default, so forEach will not work unless you define a game variable as an array beforehand.
as Poryg said beforehand; you have to define the variable as an array beforehand.
that however; is not difficult to do
Code:
$gameVariables.setValue(id,[a,b,c,d,e])
then you can do the calls normally such as forEach, Map or any other Array method
Of course! How stupid of me. The game variables are initialized in the beginning with 0, so they are integers. While one can store all kinds of stuff, even additional values, in them, the engine will still treat them as such.
So all I needed to do is to create a function that executes $gameVariables.setValue(100, []); and call it once when a new game is started. That works.
Thank you, Pyorg and pivoo.
Actually, game variables get initialized as undefined and when you use for example variable 10, but not the remaining 9, the remaining 9 get initialized as null. And whenever their value is set to "zero", it actually is set to null/undefined (depends if there is a variable of higher index being used) instead. The reason is simple. In JS every number is float, which takes 32 bits of memory, in other words 4 bytes. Null takes only one byte. They are merely manually set as integers by the engine once you use the command.
You can easily check with typing $gameVariables in console (or $gameVariables._data)
Huh... interesting, I wasn't aware of that. So since I used variable #100, simply because I didn't want it to be in between the other variables and it's far out of the way, I actually caused all the other variables before it to become initialized with NULL. I thought they're all initialized as 0 because that's what F9 shows. So they only actually become 0 rather than NULL when they are actually used? Does that include giving them a name or is it set when they are first checked?
I suppose F9 can't distinguish between 0 and NULL then?
Names are only for devs, they play no role in the actual game.
Debug shows either a real value if it exists or something called falsey value. I'm not going to teach you what falsey value is though, since that's related to javascript and not to MV.
You're right though, f9 can't distinguish between 0 and null. Namely because it doesn't care. Just like when you use for example conditional branch. Unless you specify it in script condition, it doesn't distinguish between null and 0 either. In fact even if you use $gameVariables.setValue(10, 0), the variable gets set not to 0, but to null, because it conserves memory.
Also, variables aren't really initialized. When all variables are "0", the data array is completely empty. The moment you set a variable 10 to a value though, the 9 preceding array members need to be also defined, so they get assigned default javascript value, in other words null. Again, this is nothing special, only how javascript treats arrays.
I see, that's interesting to know. Thanks for clarifying and for taking the time to explain it.
Actually, game variables get initialized as undefined and when you use for example variable 10, but not the remaining 9, the remaining 9 get initialized as null.

$gameVariables.setValue(10, 0), the variable gets set not to 0, but to null, because it conserves memory.

I'm not so sure about the "null" part, the preceding values are 'undefined', see my console screenshot below. It's the same behavior in my browser (Chrome) too with vanilla JS.

Spoiler: Screenshot
SettingVariableValues.png

And yeah like Poryg said regarding the F9 window, anytime the game checks the value of a variable (conditional branch, F9 window, etc.), it uses this function.
It checks whether the variable is "truthy" and if so, it returns the variable. But if the variable is not "truthy" (i.e. it is "falsy"), then it returns 0 instead.
Code:
Game_Variables.prototype.value = function(variableId) {
    return this._data[variableId] || 0;
};
You are right, my bad 😀

Original thread: https://forums.rpgmakerweb.com/threads/storing-arrays-persistently.91475/