Windows Saving best time after exiting game...

A

AFD456

Guest
Hi everyone, I tried to program a code for saving best time after exiting game which unfortunetaly only works when I restart a game. When I exit and run a game best time is always 0 again.

Create Event:
miliseconds = 0
time = 0

//the timer start after oTime s created
count_up = true;

ini_open( 'savebat.ini' );
if ini_key_exists('save1', 'besttime')
{
besttime = ini_read_real('save1','besttime', 0 );
}
else
{
besttime = 0
}
ini_close()

Step Event:

//If it's allowed to start
if (count_up == true) and global.dead = 0
{
miliseconds = miliseconds + delta_time/1000000
}

//if player is dead....
if global.dead = 1
{
//save to besttime
gametime = miliseconds
if gametime > besttime
{
besttime = gametime
}
highscore_add("",besttime);

ini_open( 'savebat.ini' );
ini_write_real( 'save1', 'time', besttime );
besttime = ini_read_real( 'save1', 'time', 0 );
ini_close();

//reset the time
miliseconds = 0
}

Thank you for your answers.
 

Tsa05

Member
At the create event, you do if ini_key_exists('save1', 'besttime'), which checks for a key called besttime in section save1.
But when the player gets a high score, you write ini_write_real( 'save1', 'time', besttime );, which writes besttime to a key called time in section save1.

Therefore, every time you load the game, it checks for key besttime, but only key time exists, and so it never loads the score.
You will want to change that line to ini_write_real( 'save1', 'besttime', besttime );

Also, every time you load the game, the high score table is empty by default, so you want to add a high score to it, if the high score (besttime) existed. Like this:
Code:
Create Event:
miliseconds = 0
time = 0

//the timer start after oTime s created
count_up = true;

ini_open( 'savebat.ini' );
if ini_key_exists('save1', 'besttime')
{
    besttime = ini_read_real('save1','besttime', 0 );
    highscore_add("",besttime);
}
else
{
    besttime = 0
}
ini_close()
 
A

AFD456

Guest
At the create event, you do if ini_key_exists('save1', 'besttime'), which checks for a key called besttime in section save1.
But when the player gets a high score, you write ini_write_real( 'save1', 'time', besttime );, which writes besttime to a key called time in section save1.

Therefore, every time you load the game, it checks for key besttime, but only key time exists, and so it never loads the score.
You will want to change that line to ini_write_real( 'save1', 'besttime', besttime );

Also, every time you load the game, the high score table is empty by default, so you want to add a high score to it, if the high score (besttime) existed. Like this:
Code:
Create Event:
miliseconds = 0
time = 0

//the timer start after oTime s created
count_up = true;

ini_open( 'savebat.ini' );
if ini_key_exists('save1', 'besttime')
{
    besttime = ini_read_real('save1','besttime', 0 );
    highscore_add("",besttime);
}
else
{
    besttime = 0
}
ini_close()
Works perfect so far:) Thank you for your time Tsa05. You were very helpfull!
 
Top