0 votes

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

in Engine by (15 points)

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.

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?

2 Answers

0 votes
Best answer

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
by (21,716 points)
selected by
0 votes

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.myvariable or global.myfunction()

by (91 points)

Thank you. Your explanation is really clear.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.