Luquemeister
Member
Hello! I'm trying to implement save and load in my game, is simple, I only want to save a variable (at the moment) the number of coins player has. I have an object called: obj_dataManage which is placed in the very first room of the game in the create event:
then I have the scripts save_data:
and load_data:
I call the function save_data every time the player gets a new coin in the obj coin:
the actual problem is: I load the game for the very first time, global.coins_number=0 then I play a level and for example at the end of the level global.coins_number=8, then I close the app , then I restart the game and now global.coins_number=undefined, then when a coin collide with a player this error pops up:
Does anyone knows what is happening and how can I fix this to make possible to store that data? thanks in advance
GML:
global.coins_number=0;
load_data();
Code:
function save_data(){
if(file_exists("saveGame.sav")){
file_delete("saveGame.sav");
}
ini_open("saveGame.sav")
ini_write_real("money","coins",global.coins_number);
ini_close();
}
GML:
function load_data(){
if(file_exists("saveGame.sav")){
ini_open("saveGame.sav")
global.coins_number=ini_write_real("money","coins",0);
ini_close();
}else{
//nothing
}
}
Code:
global.coins_number+=1;
save_data();
instance_destroy();
Does anyone knows what is happening and how can I fix this to make possible to store that data? thanks in advance