Save last highest score

iBack

Member
Hi I need help with saving the last highest score after the player has died.

what happens is, after the player sets a new high score (number greater to the last score) the last highest score will be shown at the main menu (I want just one last highest score to be written).

what i want is once the player picks up coin, the player will get a score of 10 (in-game), once the player dies and goes back to the main menu, at the top of page it should show the last highest score.

obj_coin2 collision event with obj_player:
GML:
instance_destroy();
with (obj_score) thescore = thescore + 10;
audio_play_sound(snd_cointest,1,0);
 

iBack

Member
Hi I need help with saving the last highest score after the player has died.

what happens is, after the player sets a new high score (number greater to the last score) the last highest score will be shown at the main menu (I want just one last highest score to be written).

what i want is once the player picks up coin, the player will get a score of 10 (in-game), once the player dies and goes back to the main menu, at the top of page it should show the last highest score.

obj_coin2 collision event:
GML:
instance_destroy();
with (obj_score) thescore = thescore + 10;
audio_play_sound(snd_cointest,1,0);
so far this adds to score in-game of 10 every time the player picks up a coin
 

SoapSud39

Member
GML:
//create
highscore = 0;

//game over condition
if thescore > highscore {
    highscore = thescore;
}
if obj_score isn't persistent then you can make highscore a global variable.

if, e.g., you wanted to display a "new highscore" message you could do something like this:
GML:
//start new game
new_highscore = false;

//game over
if thescore > highscore {
    highscore = thescore;
    new_highscore = true;
}

//draw (game over screen)
if new_highscore {
    draw_text(x, y, "New High Score!");
}
and then
GML:
//main menu
//draw
draw_text(x, y, highscore);

//OR
draw_text(x, y, "High Score: " + string(highscore));
 

iBack

Member
where to put this?
all of this inside obj_score?
for some reason once the player goes back to the screen which holds the score, it is all set back to 0.
the game over screen is the main menu which tells you the latest high score
 
Last edited:
/// @desc Autosave
if (instance_exists("Whatever the player is playing as"))
{
//Overwrite old save
if (file_exists(SAVEFILE)) file_delete(SAVEFILE);

//Create new save
var file;
file = file_text_open_write(SAVEFILE);
file_text_write_real(file,"Whatever your highscore variable is called was it thescore you said?");
file_text_close(file);
}

this is the most basic saving system I can come up with at the moment. Tell me if this helps!
 
And then you could just write something like, draw_text like @SoapSud39 did instead just write something like draw_text and then the filename? Im sorry im pretty new to game maker.
 

iBack

Member
/// @desc Autosave
if (instance_exists("Whatever the player is playing as"))
{
//Overwrite old save
if (file_exists(SAVEFILE)) file_delete(SAVEFILE);

//Create new save
var file;
file = file_text_open_write(SAVEFILE);
file_text_write_real(file,"Whatever your highscore variable is called was it thescore you said?");
file_text_close(file);
}

this is the most basic saving system I can come up with at the moment. Tell me if this helps!
where would this go?
 

iBack

Member
so far i have this:
Create event:
Code:
thescore = 0;

highscore = 0;

new_highscore = false

Step event:
Code:
if thescore > highscore {
    highscore = thescore;
    new_highscore = true;
}
draw event:
GML:
var cx = camera_get_view_x (view_camera[0]);
var cy = camera_get_view_y (view_camera[0]);
var cw = camera_get_view_width (view_camera[0]);





draw_set_font(fnt_score);
draw_set_colour(c_white);
//draw_text(cx + cw/2, cy + 25, string (thescore));

//if new_highscore {
//   draw_text(cx + cw/2, cy + 25, "High Score: " + string(highscore));
//}
if (rm_game1 || SnowStage){
draw_text(cx + cw/2, cy + 25, "High Score: " + string(thescore));
}

if (rm_title){
draw_text(cx + cw/2, cy + 25, "High Score: " + string(highscore));
}
we are close to the solution. what happens at the moment is the score gets set back to 0 once the player dies and goes back to the main menu (the game over screen is the main menu screen which tells you your last highest score)
 
hrm ok, well lets think about this for a second, the game keeps resetting and thats how it gets deleted like you said before, so obviously we can't store it in a variable like you are trying to do. so think about it like this, what if you created a variable that stores a file
//Create new save
var file;
file = file_text_open_write(save.sav);
file_text_write_real(file, thescore //or was it highscore?);
file_text_close(file);

like I said before, NOW you could make your own object for autosaving, or like you said, if the "Player" goes to different rooms you can save his score then, IDK I dont know how you want to do this, point is your gonna have to play around where you want this object to go. NOW when you have gotten your save file and want to get the high score for then you can right something like this

now let’s load the file

//Load save
var _file = file_text_open_read(save.sav);
var _highsore = file_text_read_real(file);
file_text_close(file);
and then use the variable _highscore to draw text

or something like that 😂
 
Last edited:
now that will just keep updating the score, try putting that in an object with simple EVENTS like key_press(s) first to save and then key_press(L) to load and then figure out how you want to save using other events if you get what I am saying, like just try to see if it saves and loads your highscore FIRST then come back and lets talk about loading it at the beginning of your game, tell me if it works!
 
Last edited:
I just realized, my solution WILL WORK (considering I haven’t typed anything wrong lol) but are you sure that you placed whatever that object is that is managing your high score is marked as persistent and you don’t have one of that object for each room?
 

iBack

Member
I just realized, my solution WILL WORK (considering I haven’t typed anything wrong lol) but are you sure that you placed whatever that object is that is managing your high score is marked as persistent and you don’t have one of that object for each room?
this will be the first time i have created an ini file, ive never done this before. would this just be another object?
so this would go into create.
var file;
file = file_text_open_write(save.sav);
file_text_write_real(file, thescore //or was it highscore?);
file_text_close(file)

and this will go into step event?
//Load save
var _file = file_text_open_read(save.sav);
var _highsore = file_text_read_real(file);
file_text_close(file);
and then use the variable _highscore to draw text
 

iBack

Member
I just realized, my solution WILL WORK (considering I haven’t typed anything wrong lol) but are you sure that you placed whatever that object is that is managing your high score is marked as persistent and you don’t have one of that object for each room?
yes i have marked that object as persistant and it still doesn't work.

i just tried your code in a new obj_savedata and it doesnt work.

Code:
var _file = file_text_open_read("save.sav");
var _highsore = file_text_read_real(file);
file_text_close
what is "var _highsore = file_text_read_real(file);" doing, i don't understand, game maker says "_highscore only referenced once"
where does this go? in a new object called obj_load for example?



i changed this to this:
Code:
var _highscore
var _file = file_text_open_read("save.sav");
_highscore = file_text_read_real(_file);
file_text_close(file);
 
Last edited:

iBack

Member
though i've never created an ini file before so i do not understand what this is doing. once i create both objects obj_savedata and obj_loaddata i leave this persistant in one of the rooms, but what does it do after that?
 
Nonono here is what I mean, I’m terrible at explaining things so maybe check out one of Shaun Spaldings tutorials, ANYWAY what I mean is so you have an object right, now click on add event go to key presses and key press S, now copy and paste the SAVE code in there then add another event but this time key press L, copy and paste the LOAD code in there so this is all In one event, you can then mark this persistent, the reason _highscore is only showing up once is because I created the variable _highscore for you to draw your text with, instead of using your variables thescore etc here give me a couple of hours and I’ll try and write you an object, hope it works.
 

iBack

Member
right this is what i've done, im close to the solution,


GML:
global.thescore= get_integer ("enter a score:", 0);
if global.thescore>global.highscore {

ini_open("scores.ini");
ini_write_real("scores","high",global.thescore);
ini_close();
global.highscore=global.thescore;
show_message("high score updated")

}
else
{

show_message("score is not bigger than current score");

}
what happens here is, when the score is 0 and highest score is 20 i cannot update the score with 0 since it is not greater than 20. however, when i get a score of 10 and update it overtakes the score of 20 when thats not right, as u see in the if statement anything other than thescore being higher than highscore will prompt a message of it not being higher. so anything lower than 20 or whatever score lower than high score should not update.

what is the issue here that a score of 10 overtakes and updates the highscore of 20 thats already stored.
 
Last edited:
Hrmmm maybe first try writing it like
if global.thescore<Global.highscore
And write that first then put all that’s other stuff in an else statement
 
Top