0 votes

Hello everyone, I'm fairly new to game development and I've finished making my first game, but there is only problem left, I ca't seem to get the saving/loading to work on my game here are the loading/saving scripts I'm using:

onready var death_screen = $DeathScreen
const SAVE_FILE_PATH = "user://savedata.save"
var score = 0 setget set_score
var highscore = 0

func _ready():

func _on_Ground_died():
    death_screen.init_game_over_menu(score, highscore)

func save_highscore():
    var save_data = File.new()
    save_data.open(SAVE_FILE_PATH, File.WRITE)

func load_highscore():
    var save_data = File.new()
    if not save_data.file_exists(SAVE_FILE_PATH): return
    save_data.open(SAVE_FILE_PATH, File.READ)
    highscore = save_data.get_var()

And here is the death screen script:

func init_game_over_menu(score, highscore):
    score_label.text = "SCORE : " + str(score)
    high_score_label.text = "BEST : " + str(highscore)

Whenever my player dies, I get a death screen message which shows the score correctly but the highscore value is always 0

in Engine by (20 points)

1 Answer

0 votes
Best answer

Your save_highscore and load_highscore methods both look fine.

You just need to add a check to see if your latest score is higher than your existing highscore after you die and update your highscore value if so before you save it.

Something like this would work:

func _on_Ground_died():
    if score > highscore:
       highscore = score
    death_screen.init_game_over_menu(score, highscore)
by (2,156 points)
selected by

What?! Weird is the word...

Ok, change the save and load from store_var() to store_float() - send in 42 and see what comes out. You can open up the file in notepad for good measure to see what's there if you like.

When I do that the highscore becomes equal to Null.

I finally got it to work! After changing the code to what you proposed and it didn't work, I changed it back to what it was before and everything seems to be working perfectly, I don't know what was causing the issue.

Thank you so much for your help, you are a lifesaver!

I suspect the file was corrupted. Changing the type probably meant making a new one so changing it back fixed the issue.

Anyway, great that it's working!

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.