Saving a high score
Saving the player’s high score is another common feature in many games (and one that you can add to the other games in this book as well). Since the score needs to be saved between sessions of the game, you’ll need to save it in an external file that the game can read the next time you open it.
Here’s the process:
- When the game launches, check for a save file.
- If the save file exists, load the score from it, otherwise use
0
. - When a game ends, check if the score is higher than the current high score. If it is, save it to the file.
- Show the high score on the title screen.
Since you’ll need to access the high score variable from different parts of your game, it makes sense to use an autoload. In the Script editor, click File -> New Script and name it global.gd
. To begin, you’ll need two variables:
extends Node var high_score = 0 var score_file = "user://hs.dat"