Saving and loading problem(mobile game)

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:
GML:
global.coins_number=0;
load_data();
then I have the scripts save_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();
}
and load_data:

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

    }

}
I call the function save_data every time the player gets a new coin in the obj coin:

Code:
global.coins_number+=1;
save_data();
instance_destroy();
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:
1611443602915.png

Does anyone knows what is happening and how can I fix this to make possible to store that data? thanks in advance :D
 

FoxyOfJungle

Kazan Games
You are using the same write function when loading:



Instead of writing the data, modify to ini_read_real()

GML:
global.coins_number = ini_read_real("money","coins",0);
 

curato

Member
I was too slow! I will add that I am not sure why you have to delete the file just open it and write the keys you want to write. When I have a game load I check if the file exist first and if it doesn't then I write the default values in therefore there is something to read. Then when you do the save you just need to write the keys that you need to update and it will write the file with your change no need to remove the file.
 

FoxyOfJungle

Kazan Games
I was too slow! I will add that I am not sure why you have to delete the file just open it and write the keys you want to write. When I have a game load I check if the file exist first and if it doesn't then I write the default values in therefore there is something to read. Then when you do the save you just need to write the keys that you need to update and it will write the file with your change no need to remove the file.
I recommend deleting the file every time you save, and when loading, check if the file exists as you said. This ensures that the saved file name will be completely new (this is for any file type).
 
I was too slow! I will add that I am not sure why you have to delete the file just open it and write the keys you want to write. When I have a game load I check if the file exist first and if it doesn't then I write the default values in therefore there is something to read. Then when you do the save you just need to write the keys that you need to update and it will write the file with your change no need to remove the file.
I doing because in many of the tutorials i saw they did it šŸ˜…
 

curato

Member
I recommend deleting the file every time you save, and when loading, check if the file exists as you said. This ensures that the saved file name will be completely new (this is for any file type).
Just curious why do you want a new file name everytime?
 

FoxyOfJungle

Kazan Games
Just curious why do you want a new file name everytime?
It wouldn't be a new "filename", but rather delete the entire file and save again, which is why on some consoles, while the game is saving, you can't shut down, because it would corrupt the files if you didn't finish saving, but that only happened before , files are saved instantly today. :)
Sorry, I think it was the translation that changed the word, I meant "file".
 
Top