Need help saving global variables.

C

Cdiddysir

Guest
Title says it all. I just need help saving a global variable between games. So whenever the player loads the game, the amount of how much the global variable has remains intact.

So the game starts out with the player having 0 "global.tokens" but when you pick one up it adds to the total token count. I want that total token count to be saved between games, how would I go about doing this?
 

Pfap

Member
Something like this should work.


To save:
Code:
if file_exists("save.ini"){
 ini_open("save.ini");
      ini_write_real("Save", "tokens", global.tokens);
 ini_close();
}
To load:
Code:
if file_exists("save.ini"){
 ini_open("save.ini");
   global.tokens =  ini_read_real("Save", "tokens", 0);
 ini_close();
}

Edit:
You may need to include the file for the above to work, or else omit the if file_exists() check. I'm not sure if Gamemaker creates a new file if you try to open a non existing one.
 
C

Cdiddysir

Guest
Where exactly would I put all of that code? I have the global tokens set in the ob_game object, and if the player interacts with the ob_token, then their global.token count increases by 1
 

Pfap

Member
Where exactly would I put all of that code? I have the global tokens set in the ob_game object, and if the player interacts with the ob_token, then their global.token count increases by 1
If the data is important you could save it any time it is changed if you wanted to, so you would put the to save code block right after changing the variable.

Right after you change the global.tokens variable you would run this:
Code:
if file_exists("save.ini"){
ini_open("save.ini");
      ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
ini_close();
}
 
C

Cdiddysir

Guest
Cool, and then I would load it in the ob_game object right?
 
C

Cdiddysir

Guest
Code:
//create event for ob_game_menu (The first object in the game, which leads to the main menu upon startup)

if file_exists("save.ini"){
 ini_open("save.ini");
   global.tokens =  ini_read_real("Save", "tokens", 0);
 ini_close();
}


Code:
//create event for ob_game

global.tokens = 0

Code:
///player collision event with ob_token

global.tokens++;

instance_destroy(other);
audio_play_sound(a_token,10,false)


if file_exists("save.ini"){
ini_open("save.ini");
      ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
ini_close();
}
 

pipebkOT

Member
To load the game
Code:
//create event for ob_game_menu (The first object in the game, which leads to the main menu upon startup)

if file_exists("save.ini"){
 ini_open("save.ini");
   global.tokens =  ini_read_real("Save", "tokens", 0);
 ini_close();
}

else
{
//Inits all variables

global.tokens=0
}

DELETE THIS:
//create event for ob_game

global.tokens = 0
because you are overwriting the variable back to zero:rolleyes: and you don't want that!



To save the game:

Code:
ini_open("save.ini");
      ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
ini_close();
}
 
Last edited:
Top