Can't save my Highscore

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

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()

Where do you call save_highscore()?

jgodfrey | 2020-11-26 15:34

:bust_in_silhouette: Reply From: jgodfrey

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.

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

Rayu | 2020-11-27 11:18

Where do you call load_highscore()?

jgodfrey | 2020-11-27 15:37

Ups…

I just created a ready function for load_highscore()

sorry…

Rayu | 2020-11-27 17:52