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

GML My bestScore variable keeps resetting

ElCheTibo

Member
Hello everyone,

I'm currently working on a simple top-view shooter. I've decided to add a score and best score feature to it.
When the current room is rGameOver, the score is displayed, and if the player presses space, the game restarts...

Unfortunately, I can't stop my bestScore variable to reset after pressing space!

Here are the code snippets:
GML:
// Create event of oGameManager

bestScore = 0;
GML:
// Step event of oGameManager

if (room == rGameOver)
{
    if (score > bestScore) bestScore = score; // if the current score is higher than bestScore, it stores the score's value
    
    if (keyboard_check_pressed(vk_space)) // the game resets these variables to their default values
    {
        diff = 1;
        score = 0;
        lives = 100;
        game_restart(); // i said nowhere to reset bestScore :/
    }
}
What am I doing wrong? Am I forgetting an else statement somewhere?
 

TsukaYuriko

☄️
Forum Staff
Moderator
You're forgetting that there is bestScore = 0; somewhere and that it will be triggered when you restart the game. ;)

If you want this to persist between runs of the game (including restarts), save it to a file. If you don't want that, ensure that the initial declaration only runs once - for example by putting whatever declares it into the first room of the game and visiting it no more than once during the game's run time.
 

ElCheTibo

Member
You're forgetting that there is bestScore = 0; somewhere and that it will be triggered when you restart the game. ;)

If you want this to persist between runs of the game (including restarts), save it to a file. If you don't want that, ensure that the initial declaration only runs once - for example by putting whatever declares it into the first room of the game and visiting it no more than once during the game's run time.
How can I make a file in GML?
 

FrostyCat

Redemption Seeker
Is it normal these days for "students" to be 4 months into GML and still not know how to handle a file?
GML:
ini_open(working_directory + "save.ini");
bestScore = ini_read_real("scores", "best", 0);
ini_close();
GML:
ini_open(working_directory + "save.ini");
ini_write_real("scores", "best", bestScore);
ini_close();
 

TsukaYuriko

☄️
Forum Staff
Moderator
... or not to know how to use the manual? :)

Search -> "file". file_text_write_real is the second result, and the page that lists all file functions is only a click away from that page.
 
Top