• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Saving value when MOBILE app is closed

AcrobatEpee

Member
Hello, as said in the title, how do I save a value and make it appear when the app reopens, global variables are made just for room no ? Thanks for your help :)

I want to save the global variable named
global.scr
 
Last edited:
J

J_Dev

Guest
Can you explain this to me? You want to detect when the app loses focus and then when the user reopens the app display a message?
 

AcrobatEpee

Member
Hey J_Dev ! No, I have a highscore in my app, and I just want that when the app reopens my highscore shows. I can't find a way to save it so it is still available once the app has been closed
 
J

J_Dev

Guest
Hey J_Dev ! No, I have a highscore in my app, and I just want that when the app reopens my highscore shows. I can't find a way to save it so it is still available once the app has been closed
Oh ok so you are looking to save the data. Well to do that you should look at .ini files. The idea is to save the highscore to a file, then when the player reopens the game read the file and set your highscore variable to what is saved.

When saving it will look something like this:
Code:
score = 1000;
ini_open("savedata.ini");
ini_write_real("save1", "Highscore", score );
ini_close();
Then when loading:
Code:
ini_open("savedata.ini");
score = ini_read_real("save1", "Highscore", 0 );
ini_close();
This obviously isn't a thorough explanation, and you should look through the documentation to make sure you understand what is going on. But ultimately, it isn't very complicated and it will achieve what you are looking for.
 
E

Edwin

Guest
What does that mean ?!?
You can't delete INI files that you created with your game in unrooted device, but I think you can delete them in-game.

Root explaining:
First of all, root allows you to delete standard applications imposed by device manufacturers, change themes and shortcuts, as well as launch specialized applications that significantly expand the capabilities of a smartphone (usually such applications require Root rights), etc. In short: with rooted device you can perform all operations without exception.
 

AcrobatEpee

Member
Oh yeah I get it so people can modify the files as they want if their phone is rooted. That’s no problem to me I’ll let them be and let them cheat :)

That said, I can just basically call my value everytime I need to display my highscore right ? (Because I can’t know when the user will quit the app so I can save the value in the .ini file, so can I use it like my global variable without any problem ?)
 
E

Edwin

Guest
Oh yeah I get it so people can modify the files as they want if their phone is rooted. That’s no problem to me I’ll let them be and let them cheat :)

That said, I can just basically call my value everytime I need to display my highscore right ? (Because I can’t know when the user will quit the app so I can save the value in the .ini file, so can I use it like my global variable without any problem ?)
Global variable will reset after restarting game, but you still can set your global variable to INI file value that you created. By the way there are event called game end. It calls only when player is quitting the game and you can save your values in INI file when it's calling.
 

AcrobatEpee

Member
Oh didn’t know about it. Thanks a lot :)

Also, just to be sure, in this code
Code:
score = 1000;
ini_open("savedata.ini");
ini_write_real("save1", "Highscore", score );
ini_close();
save1 is the name of the saved value ? Just like if it was a variable in the ini file ?
 

AcrobatEpee

Member
I did this :
Code:
var comp;

ini_open("savedata.ini");
comp = ini_read_real("save1", "Highscore", 0 );
ini_close();
highscore = comp;

if (global.scr > comp){

highscore = global.scr;
ini_open("savedata.ini");
ini_write_real("save1", "Highscore", global.scr );
ini_close();

}
I want it to call the saved value, compare it to the global.scr.
If the global.scr is higher than the saved value, it overwrites the value and saves the highscore variable to global.scr (and otherwise saves the highscore variable to the saved value)

Is that correct ?
 
E

Edwin

Guest
Oh didn’t know about it. Thanks a lot :)

Also, just to be sure, in this code
Code:
score = 1000;
ini_open("savedata.ini");
ini_write_real("save1", "Highscore", score );
ini_close();
save1 is the name of the saved value ? Just like if it was a variable in the ini file ?
save1 is your section name, not value name. You should write there something like GENERAL or VALUES. This is how your INI file will look like:
upload_2018-11-12_1-22-2.png
 
E

Edwin

Guest
I did this :
Code:
var comp;

ini_open("savedata.ini");
comp = ini_read_real("save1", "Highscore", 0 );
ini_close();
highscore = comp;

if (global.scr > comp){

highscore = global.scr;
ini_open("savedata.ini");
ini_write_real("save1", "Highscore", global.scr );
ini_close();

}
I want it to call the saved value, compare it to the global.scr.
If the global.scr is higher than the saved value, it overwrites the value and saves the highscore variable to global.scr (and otherwise saves the highscore variable to the saved value)

Is that correct ?
Yes, it should work.
 

TheouAegis

Member
Although... Do the game_end and cleanup events fire when the app is legitimately closed via the phone's task screen? you can change focus, and the game will keep running. But if you change focus, go into the task screen, and close the game, since the game does not have focus at that point, isn't that the same as closing the program from the Task Manager in Windows, which does not fire off any event?

For his high score table thing, it shouldn't really even matter, but for other features like automated saving, I would assume it's pretty important to know.
 
Top