Persistent Global Variable

Hello there everybody, I'm learning Javascript little by little but I can't seem to find a solution to this problem: I would like a global variable initialized to a value the first time you run the game, but I need it to be persistent. So for example when this variable is changed in-game, and the game is closed, when you run it again the variable must have the value changed in-game and not its default one.

Maybe it is an obvious question for you but I have some difficulties. How can I implement it?
Thanks in advance.

EDIT: look the 4th post (mine) for more details of what I'm trying to achieve
Tsukihime has written a plugin to syncronise variables over different save games, you can find it on her blog.
Nothing short of a plugin can do that, because the game variables are saved into the savefiles and initiated on every new game, so that they depend on their savefiles status which OS different for different new games.
If you were asking about just making it persistent within one save file, then you either need to make the variable a property of one of the object classes that gets saved with the savefile (for example, $gameSystem), or you need to change DataManager.makeSaveContents() and DataManager.extractSaveContents() to include your variable.
@Andar I checked it and it solved another problem I had (thanks!), but it is limited as switches/variables in game I see...

I can explain what I'm trying to achieve, so maybe you can help me directly with that: I want to change the images in the title screen when the main story is over, and I must do it by editing a Plugin for the title screen (I use a custom title screen by MOG, called "Title Layers" along with "Title Particles. The last one was unnecessary to say, but I included it just in case..). So I have to check a particular variable (the "persistent global" I mentioned before) exactly at game launch. By passing the $gameSwitches.value[x] at game launch, it doesn't get the correct value obviously even without Hime's Plugin.

So, do you think that there is a way to do it?
@Llareian if you have any ideas too 😀
Thanks a lot for your time and patience..

(PS: I already tried a Plugin by MrTrivel but it has some issues running with the MOG's Plugin mentioned before unfortunately)
hi
for build a persitance variable just do like this.

*Create a newFile.JS
*Add newFile.JS to you plugin manager (top)
*Add this code in...
PHP:
function _myVarName() { // prototype name of myVarName
       // stuff initialise only if var not existe
};

var myVarName = myVarName || new _myVarName(); // asign or create if not exist


//After make fun to add some method to you variable
_myVarName.prototype.doSomething = function(arg) {
   //stuff, "this." will after the current variable
};

to see if variable stored and existe or it a gobal, and also see all data asigned.
tape window.myVarName or myVarName in the console from rmmv

After you can acces to this var any where you need in game
@Jonforum my apologies but I think I didn't get what you were trying to explain. I put your code exactly as it is in a new JS plugin, but everytime the game is started, the variable is re-initialized. So, I don't know maybe I make something wrong?
@Jonforum my apologies but I think I didn't get what you were trying to explain. I put your code exactly as it is in a new JS plugin, but everytime the game is started, the variable is re-initialized. So, I don't know maybe I make something wrong?
a persistance variable its a variable , that will never erase in game or will not erase after scope.
What your seem to talk, its to save a variable in a JSON, and after when close and start game , load the JSON if exist.
Try export the variable in a JSON, and after load befor this code.
Make sure use the same syntax for creat variable.
PHP:
var myVarName = loadMyCustomJson();
myVarName = myVarName || new _myVarName();
Sorry for the late reply but I was overwhelmed by Christmas etc... and I am sorry again but I can't understand what to do. I am a newbie of Javascript and I don't understand the examples you provided unfortunately. Could you show me exactly how I put a variable called "myGlobalVar" so that it works in another .js plugin without further implementation?
I can't understand what to do. I am a newbie of Javascript and I don't understand the examples you provided unfortunately.?

you need study javascript ! lol
https://www.lynda.com/search?q=javascript&f=level:1^Beginner

I know it a little but maybe I don't understand your examples or how the language is related to RMMV. If you can't do that nevermind, I will try to do it on my own hopefully before my game is ready... thanks anyways!

for mods: you can close the topic
I know it a little but maybe I don't understand your examples or how the language is related to RMMV. If you can't do that nevermind, I will try to do it on my own hopefully before my game is ready... thanks anyways!

for mods: you can close the topic
ok, i'm sorry, sometimes we get to help, sometimes no.
you can try this , it work my side.
PHP:
//Create a javascript object with the table array in it

var obj = {
   table: []
};
//Add some data to it like

obj.table.push({id: 1, square:2});
//Convert it from an object to string with stringify

var json = JSON.stringify(obj);
//use fs to write the file to disk

var fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8', callback);
//if you want to append it read the json file and convert it back to an object

fs.readFile('myjsonfile.json', 'utf8', function readFileCallback(err, data){
    if (err){
        console.log(err);
    } else {
    obj = JSON.parse(data); //now it an object
    obj.table.push({id: 2, square:3}); //add some data
    json = JSON.stringify(obj); //convert it back to json
    fs.writeFile('myjsonfile.json', json, 'utf8', callback); // write it back
}});

more info
https://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs/
So you want to change the title image when the player reaches a certain point in the story. Is it dependent on save files?
What I mean is:
  • If a player finishes the main story in Save File 2 but not Save File 1, which title image should show?
  • If the player now deletes Save File 2 because they want to replay the story differently, which title image should show?
  • If the player finishes the main story but forgets to save, which title image should show?
If it is completely independent of the save file then you should go with Jon's strategy. Create/modify a JSON file when the player finishes the main story. A JSON file is nothing but a fancy text file that can be easily read in JavaScript. On the title screen, access the JSON file to check whether the player finished the main story and display the image accordingly.


for mods: you can close the topic
If you want the topic to be closed, report the first post asking for it to be closed. It's the only way the mods will see it.
oups

Original thread: https://forums.rpgmakerweb.com/threads/persistent-global-variable.88696/