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

How to do a highscore?

F

#Frogglets ;-;

Guest
Hi,
for my game I want one highscore to appear which is your personal best rather than a whole table. My code was:

with (obj_player)
if obj_player.hp <= 0
{
global.finalscore += global.thescore;
}

and:

if global.finalscore >= global.highscore
then{
global.highscore = global.finalscore;
draw_text(100,100,string(global.highscore));
}

draw_text(100,100,string(global.highscore));

whenever I try to do this tho my highscore equals out to 0 everytime and I dont know how to resolve it. Any help would be greatly appreciated!!!
Thanks, A_Froggo
 
T

The Shatner

Guest
Hey there!
I would suggest debugging it. Something might be happening with the global.finalscore or the global.thescore vars.
Try printing them onscreen at all times during the game and see if they are above 0.
For example:
In the obj_player DRAW EVENT:
Code:
draw_self();
draw_text(100,100,"finalscore is "+string(global.finalscore));
draw_text(100,150,"thescore is "+string(global.thescore));
Also, avoid doing calculations on the Draw event. Move them to the Step Event and just show the results on the Draw Event. Too many calculations inside the Draw Event may slow down the game and cause other issues.
 
F

#Frogglets ;-;

Guest
h
Hey there!
I would suggest debugging it. Something might be happening with the global.finalscore or the global.thescore vars.
Try printing them onscreen at all times during the game and see if they are above 0.
For example:
In the obj_player DRAW EVENT:
Code:
draw_self();
draw_text(100,100,"finalscore is "+string(global.finalscore));
draw_text(100,150,"thescore is "+string(global.thescore));
Also, avoid doing calculations on the Draw event. Move them to the Step Event and just show the results on the Draw Event. Too many calculations inside the Draw Event may slow down the game and cause other issues.
hi The Shatner!!
so, whenever I go to a new room my score is set to 0 and my finalscore is either not getting updated or getting set back to 0.
Any idea how to resolve it??
 
B

Bayesian

Guest
so, whenever I go to a new room my score is set to 0 and my finalscore is either not getting updated or getting set back to 0.
Any idea how to resolve it??
You most likely have global.finalscore = 0 in some object's create event. If that object is not persistent then you are also most likely creating it again when you change rooms which will run the create event again setting it to 0 each time. So the options are to make the object that initializes global.finalscore persistent or have a persistent controller object initialize it or have an if statement check if it = undefined
 
F

#Frogglets ;-;

Guest
You most likely have global.finalscore = 0 in some object's create event. If that object is not persistent then you are also most likely creating it again when you change rooms which will run the create event again setting it to 0 each time. So the options are to make the object that initializes global.finalscore persistent or have a persistent controller object initialize it or have an if statement check if it = undefined
HI!!!
I got it to work by having a room at the start of the game with all my persistant vars in it!
Thanks for the help though! I really do appreciate it!
 
T

The Shatner

Guest
Good to know you're on the right path, Frogglets!
Another suggestion I would give you is to save/fetch the data from an INI file. For example:
Code:
finalscore = global.finalscore;
ini_open("data.ini"); //this can be any filename, such as "custom.ini" or "imissthegoodolddays.ini", it will be created in case it doesn't exist
comparerscore = ini_read_real("hiscore","hiscore",0); //this tells GMS to read what's in the "hiscore/hiscore" part of your INI file. In case it's not existant, it will return "0"
if (finalscore > comparerscore) { //that is, if your current finalscore is higher than the one previously saved
ini_write_real("hiscore","hiscore",real(finalscore); //writes the new record to the INI file
}
ini_close(); //always close your INI to avoid memory leaks
This way, you can close your game and the highscore will always be there.

For more info on the INI saving system, check https://docs.yoyogames.com/source/dadiospice/002_reference/file handling/ini files/index.html (GMS 1.x) or https://docs2.yoyogames.com/source/..._reference/file handling/ini files/index.html (GMS 2.x).

I must remind you that the INI can be easily edited on many platforms, so for advanced usage you'll have to implement an encryption method to avoid user "tampering". However, that is usually only necessary on commercial projects and the like. And encryption is quite a difficult thingy to implement on GMS.
 
Top