Legacy GM Saving highest score to ini file

M

Matt93

Guest
I'm trying to do something really simple, but have never used ini files before so can't work it out.

When the player loses all their lives, their score saves to an ini file:

Code:
if (lives < 1) {
if (file_exists("Save.sav")) file_delete("Save.sav");
ini_open("Save.sav"); //opens file for writing and reading. If doesn't exist, Game Maker creates it

var highscore = score;
ini_write_real("Save1", "score", highscore);
ini_close();
}
I only want this to overwrite the previous score if it is higher though, as as the moment it just writes the most recent score. I know this will be a really simple solution, but any help with this would be useful. Cheers!
 
M

Matt93

Guest
Do a check first, if score > ini_read_real("Save1", "score", 0)
//overwriting code
This is my code now, but it still isn't working. Thank you for your suggestion, do you have any ideas?

Code:
if (lives < 1) {
if (file_exists("Save.sav")) file_delete("Save.sav");
ini_open("Save.sav"); //opens file for writing and reading. If doesn't exist, Game Maker creates it

var highscore = score;
if (score > ini_read_real("Save1", "score", 0)) ini_write_real("Save1", "score", highscore);
ini_close();
}
 

amusudan

Lousiest of Potatoes
Code:
if (lives < 1) {
if (file_exists("Save.sav")) 
{
file_delete("Save.sav");
}

ini_open("Save.sav"); //opens file for writing and reading. If doesn't exist, Game Maker creates it

if (score > ini_read_real("Save1", "score", 0))
{
ini_write_real("Save1", "score", score);
}
ini_close();
}
It should work, and you don't need to set the var highscore and write that first, just write score. Bt this code should work (it's not modified other than the var thing)
 
R

Rusty

Guest
You don't need to do a file_delete. If you are saving to the same section and key then it will simply overwrite the existing value in that key.

You only need:
ini_open("Save.ini")
if highscore>ini_read_real("Save1","score",0)
{ ini_write_real("Save1","score",highscore);}
ini_close();

Edit:
And obviously, like Amusudan included but for some reason I didn't, you need to activate it when "lives=0". I'm not sure why you're doing "lives<1".
 

amusudan

Lousiest of Potatoes
Maybe the problem is somewhere else? In your loading code
You don't need to do a file_delete. If you are saving to the same section and key then it will simply overwrite the existing value in that key.

You only need:
ini_open("Save.ini")
if highscore>ini_read_real("Save1","score",0)
{ ini_write_real("Save1","score",highscore);}
ini_close();

Edit:
And obviously, like Amusudan included but for some reason I didn't, you need to activate it when "lives=0". I'm not sure why you're doing "lives<1".
I assumed that maybe he would write different things to the file too in other execute code's, that's why I didn't mention it ;).
 
R

Rusty

Guest
Still, there is no reason to do a file_delete to erase the entire file. All the values can simply be overridden. Deleting the file in this instance is just bad practice as far as I can tell.
 
M

Matt93

Guest
Thank you so much everyone, got it working. Have no idea why I decided to put that file_delete in there in the first place.

Code:
if (lives == 0) {
if (file_exists("Save.sav")) ini_open("Save.sav"); //opens file for writing and reading. If doesn't exist, Game Maker creates it //file_delete("Save.sav");

var highscore = score;
if (highscore > ini_read_real("Save1", "score", 0)) ini_write_real("Save1", "score", highscore);
ini_close();
}
 
  • Like
Reactions: Cmi

amusudan

Lousiest of Potatoes
Sometimes it can be necessary, depending on the other code that we're not seeing and as this is only exectued once when the game ends (Atleast, I assume so) it won't be that much of a problem. Unless ofcourse he is only writing this one variable then it is definitely not good practice.
 

amusudan

Lousiest of Potatoes
Oh, good! You got it to work :D.

Good luck with your project, and if there's any other problems the GMC is here 24/7/365 ;)
 
R

Rusty

Guest
Code:
if (lives == 0) {
if (file_exists("Save.sav")) ini_open("Save.sav"); //opens file for writing and reading. If doesn't exist, Game Maker creates it //file_delete("Save.sav");

var highscore = score;
if (highscore > ini_read_real("Save1", "score", 0)) ini_write_real("Save1", "score", highscore);
ini_close();
}
Your code isn't quite perfect yet. The GML for this should be:
Code:
if lives = 0
{  ini_open("Save.sav");
  if score>ini_read_real("Save1","score",0) 
  {  ini_write_real("Save1","score",score);}
  ini_close();}
(ignore my formatting)

But yes, you do not need to use a "var" in this code as the existing variable does not change during the code, so you do not need to store it. Also, "file_exists" does not need to be used as it does not effect if you save to the file or not. It is a pointless check in this instance.

A better example of when to use "file_exists" is when you are creating these files for the first time. So for example, you use "file_exists" to see if a player score exists and if not, you set up the default scoreboard. You do not need to use file_exists to write into an ini file.
 
  • Like
Reactions: Cmi
M

Matt93

Guest
Your code isn't quite perfect yet. The GML for this should be:
Code:
if lives = 0
{  ini_open("Save.sav");
  if score>ini_read_real("Save1","score",0)
  {  ini_write_real("Save1","score",score);}
  ini_close();}
(ignore my formatting)

But yes, you do not need to use a "var" in this code as the existing variable does not change during the code, so you do not need to store it. Also, "file_exists" does not need to be used as it does not effect if you save to the file or not. It is a pointless check in this instance.

A better example of when to use "file_exists" is when you are creating these files for the first time. So for example, you use "file_exists" to see if a player score exists and if not, you set up the default scoreboard. You do not need to use file_exists to write into an ini file.
So, even if "Save.sav" hadn't been created yet, ini_open would just create this for me, rather than throwing an error?
 
R

Rusty

Guest
So, even if "Save.sav" hadn't been created yet, ini_open would just create this for me, rather than throwing an error?
Exactly. Even if you tried to read from an ini file that didn't already exist, it would return with the default value instead of giving you an error.

For example for:
ini_read_real("Save1","score",60);

If the ini file didn't exist or if this key didn't exist in the ini file, it would return a default value of 60 instead of giving you an error code.

Management of ini files is probably one of GML's most forgiving aspects.
 
M

Matt93

Guest
That makes perfect sense! I think I've just grown extra wary of Game Maker throwing errors for non-existent variables. I'll definitely remember this about ini files for the future :)
 
  • Like
Reactions: 607
Top