• 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!

Legacy GM [SOLVED]How to save actual score with ini_file

Y

Ysela_Creyo

Guest
Hi, I'm using ini_file, to save and load my current game, i'm having troubles trying to save the score.

The main problem is the game save and load the default game score, let's say it's "2000", (this value is set in my object code score = 2000) everytime game load the save.sav, it loads that score, but I want the game load/save the last actual score, let's say "1250" for example (the one showing in the drawing during the gameplay in the room). i have this script to make the game save (when game end) and load (when game start)

if (file_exists("save.sav")) file_delete("save.sav");
ini_open("save.sav");
var SavedRoom = room;
ini_write_real("save1", "room", SavedRoom);
ini_write_real("save1","score",score);
ini_close();

if (file_exists("save.sav"))
{
ini_open("save.sav");
var LoadedRoom = ini_read_real("save1","room","ingame");
score = ini_read_real("save1","score",0)
ini_close();
room_goto(LoadedRoom)
}
else
{
// do nothing
}

These script's are executed by load and save objects in my game.
 
T

Token Chingy

Guest
From what I can see, your save and load functions are working perfectly fine (I don't have GMS around to test it on but the logic seems sound). What is the actual problem?

What my understanding is that you have a score the player has achieved, on the games exit, it saves this score to the .ini file. On the game start, it reads the same file and loads the score. Is that working or not?
 
Y

Ysela_Creyo

Guest
From what I can see, your save and load functions are working perfectly fine (I don't have GMS around to test it on but the logic seems sound). What is the actual problem?

What my understanding is that you have a score the player has achieved, on the games exit, it saves this score to the .ini file. On the game start, it reads the same file and loads the score. Is that working or not?
Yeah it's saves the save.sav file fine, i check it with Notepad++ and was fine. Then problem is this. let's make this example, the score default is set to 0, i want when the game end, save the current score the player win during the game for example let's say won 100. but when load the game, the score is 0 (default), in change of being 100 (the one won during last game)
 
T

Token Chingy

Guest
Okay yea so it's defaulting to 0 (That's what you set the default value as if the value to return if a value is not found in the defined place (or the .ini file does not exist). Must be a real number.).

What's the structure of the ini when you save it?
 

chamaeleon

Member
Perhaps it would be worthwhile to put in a show a debug message in your load function where you print out the score after it is loaded. You haven't indicated in your messages that you know for a fact the ini_read_real() for score has actually executed and stored the value where you expect it to be.
 
T

Token Chingy

Guest
Perhaps it would be worthwhile to put in a show a debug message in your load function where you print out the score after it is loaded. You haven't indicated in your messages that you know for a fact the ini_read_real() for score has actually executed and stored the value where you expect it to be.
Unless his scores actual default as specified in his code is 0, then I would assume it has executed and has not found the data in the ini structure and thus default the score to 0.

EDIT: Stupid question, is that object persistent?
 

chamaeleon

Member
Unless his scores actual default as specified in his code is 0, then I would assume it has executed and has not found the data in the ini structure and thus default the score to 0.
That is true.. If it is reading, but not finding the value, it'd use 0 as the default. Still, personally, if I'm not getting an expected value, I'd want to make sure the code lines I expect to execute are in fact executed.

@Ysela_Creyo Side note, the reading of LoadedRoom with ini_read_real() and supplying a string as default does not look accurate. You probably would want to save the name of the room rather than its id, which could change if you add rooms between creating save games and look up the room using asset_get_index() rather than relying on numbers being stored.
 
Y

Ysela_Creyo

Guest
Unless his scores actual default as specified in his code is 0, then I would assume it has executed and has not found the data in the ini structure and thus default the score to 0.

EDIT: Stupid question, is that object persistent?
Wait, this time I check carefully during the gameplay the score first show the last one 1925 (like for a second) then later become the defualt one) and in the save.sav file, the score is the last one saved 1925 (not the default this time). Why the score still change to the default?
 
T

Token Chingy

Guest
Wait, this time I check carefully during the gameplay the score first show the last one 1925 (like for a second) then later become the defualt one) and in the save.sav file, the score is the last one saved 1925 (not the default this time). Why the score still change to the default?
It's not your load code. Do you have an initializer anywhere that could reset the score?
 
Y

Ysela_Creyo

Guest
It's not your load code. Do you have an initializer anywhere that could rest the score?
I don't have anything to that can "rest" the score (not sure what you mean); I have a controller (object) that when the game start, set the score to 2000. The point of the Ini_file is to can save the actual score.

I guess may this cause the score to set again to defualt in change to the one in the ini_file?
 

chamaeleon

Member
I don't have anything to that can "rest" the score (not sure what you mean); I have a controller (object) that when the game start, set the score to 2000. The point of the Ini_file is to can save the actual score.

I guess may this cause the score to set again to defualt in change to the one in the ini_file?
Just setting the score to 2000 wouldn't cause your ini file to be updated. Only calling your script code that does the writing would do that. I will reiterate I would personally start adding debug statements wherever the score variable is used in your code to print out what code segment you're in and what the value is, in order to figure out if there's an unexpected value showing up at any point, or if there is a debug statement that should show up but doesn't, indicating that a critical code path has not been executed.
 
Y

Ysela_Creyo

Guest
Just setting the score to 2000 wouldn't cause your ini file to be updated. Only calling your script code that does the writing would do that. I will reiterate I would personally start adding debug statements wherever the score variable is used in your code to print out what code segment you're in and what the value is, in order to figure out if there's an unexpected value showing up at any point, or if there is a debug statement that should show up but doesn't, indicating that a critical code path has not been executed.

Thank you @chamaeleon.696 and @token-chingy.18338 I already fix it, the problem was pretty simple (that I didn't realize) :p

if (file_exists("save.sav"))
{
ini_open("save.sav");
score = ini_read_real ("save1", "score", 0);
ini_close();
}
else
{
score = 2000
}

This is how I fix it, before I have only "score = 2000" set in my main controller of the game, which will set the score to always to the default, even if the .ini_file want to change.

So i write to the controller to read the ini_file score firts, (if file exist) else set the score to 2000; then it works.
 
Top