Save Game code - Where to place and how to access?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Swirly cogs

Hi all. I’m a new beginner to both coding and game engines so please bear with me.

I completed the “dodge the creeps” tutorial and have been working to expand it by learning new things. From browsing around i see lots of information about saving the high score and believe i have an understanding of the code but am stumped by where to add it in the code/scenes. does it need to be in a global script by itself or can it be placed in any scene? also how do i access it to say update a label on my menu screen with the highest score?

Thanks in advance

What do you mean by you “have understanding of the code”? I don’t think you’d want to put it in any scene (Enemy for example). There’s not really a need for a global script, since this game only has one running scene anyway.

There are two parts to the process:

  • At application start, read the highscore from the score file (in _ready() for example)
  • At game end, if score > highscore set highscore and save it to score file (in game_over() for example)
  • Set the label text using the highscore variable

If you can update your post with more specific question(s) I’ll be happy to help you with them.

kidscancode | 2018-09-10 20:32

Thanks for replying. Your videos are one of the main reasons i’m here as i find them the best to be able to watch. Fan moment over :

By understanding i mean that i understand where you have to make the file and check if it exists already then read from it and write to it (maybe i don’t understand it as in depth as i should).

I believe just the processes you have mentioned above have answered what i was after. For the first two points would this all be inside the ready and game over script in the main scene?

For the label text do i just retrieve the highscore from the save file with code scripted in the label itself?

Swirly cogs | 2018-09-10 20:58

:bust_in_silhouette: Reply From: vspin

You can add a node to the scene and attach a script to it but if you change scenes, the data is lost. You can create a global script, then go to Project settings, then click the auto load horizontal tab, and add your global script which will allow you to access this data from anywhere without losing the data. To reference a variable or function from another script, just simply prefix with the name of your global script like this: global.my_variable or global.my_function()

Thank you. Your explanation is really clear.

Swirly cogs | 2018-09-10 21:04

:bust_in_silhouette: Reply From: kidscancode

Yes, do it all in the main scene - it’s the only scene you have anyway (in the “Dodge the Creeps” tutorial, that is). You only need to retrieve the score from the file once - when you launch, so do that in _ready().

From then on, you have it in a variable, highscore for example. Set the label’s value with

$SomeLabel.text = "High Score: " + str(highscore)

When the game ends, do something like

if score > highscore:
    highscore = score
    # write value to save file