0 votes

I've made a Gameover Menu where you can see the Score and Highscore. It works fine
but when i restart the Game the Highscore is 0

extends Control


const SAVE_FILE_PATH = "user://savedata.save"

func game_over():
    if Globals.Score > Globals.highscore:
        Globals.highscore = Globals.Score


func save_highscore():
    var save_data = File.new()
    save_data.open(SAVE_FILE_PATH, File.WRITE)
    save_data.store_var(Globals.highscore)
    save_data.close()

func load_highscore():

    var save_data = File.new()
    if save_data.file_exists(SAVE_FILE_PATH):
        save_data.open(SAVE_FILE_PATH, File.READ)
        Globals.highscore = save_data.get_var()
        save_data.close()
in Engine by (133 points)

Where do you call save_highscore()?

1 Answer

+3 votes
Best answer

I just tested your code and it seems to work as intended. So, I can only assume that you're not actually calling save_highscore()? At least you're not calling it in the code you posted.

by (10,914 points)
selected by
extends Control


const SAVE_FILE_PATH = "user://savedata.save"

func game_over():
    visible = true
    if Globals.Score > Globals.highscore:
        Globals.highscore = Globals.Score
        save_highscore()
    $VBoxContainer/Best.text = "Best  " + str(Globals.highscore)


func save_highscore():
    var save_data = File.new()
    save_data.open(SAVE_FILE_PATH, File.WRITE)
    save_data.store_var(Globals.highscore)
    save_data.close()

func load_highscore():

    var save_data = File.new()
    if save_data.file_exists(SAVE_FILE_PATH):
        save_data.open(SAVE_FILE_PATH, File.READ)
        Globals.highscore = save_data.get_var()
        save_data.close()

really? when i close the game and start again the Best is 0
im not sure but maybe it has something to do with this:

$VBoxContainer/Best.text = "Best  " + str(Globals.highscore)

but i tried to fix this doing things like

var highscore = Globals.highscore

so the string doesn't load always the Globals.highscore from the Globals.Script.
but idk how to fix that

Where do you call load_highscore()?

Ups...

I just created a ready function for load_highscore()

sorry...

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 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 webmaster@godotengine.org with your username.