How to create an high score timer(label) and save

Godot Version

3

Hi,I want to create another timer(label) to copy the first timer(label) then when die to save the high score.I want to create a browser game so as i understand i need to use java or other language, so if anybody will help me i will be very happy.

That’s my main timer

extends Label

var time_alive: float = 0.0
var running: bool = true

func _ready():
	# Set the initial text of the Label
	text = str(int(time_alive))
	# Start processing every frame
	set_process(true)

func _process(delta):
	if running:
		# Increment the time_alive by the time passed since the last frame
		time_alive += delta
		# Update the Label's text to show the elapsed time in seconds
		text = str(int(time_alive))

func stop_timer():
	running = false

and that’s the timer that copies the main timer time but needs to save it as high score

extends Label
	
func _physics_process(delta):
	var timer_text = get_node("../Timer1").text
	self.text = "High Score: " + timer_text

How is this different from this post you’ve made before? You do not need to learn Java to make a web browser game. Have you tried exporting your game for the web?

Its mostly the same just added to stop when my player dies, that means i can just use my players local storage to save high score in gdscript?

Yes you can and should store player data through Godot, such as gdscript.

Since you only want to store the time_alive you can use these functions to save and load. If you use a path with “user://” it will save the data locally, even to the web browser.

func save_highscore(highscore: float) -> void:
	var file := FileAccess.open("user://savefile.dat", FileAccess.WRITE)
	if file:
		file.store_float(highscore)
	else:
		var err := FileAccess.get_open_error()
		push_warning("Could not open file: ", error_string(err))


func load_highscore(default_value: float = 0.0) -> float:
	var file := FileAccess.open("user://savefile.dat", FileAccess.READ)
	if file:
		return file.get_float()
	else:
		# Could not open, might not exist
		return default_value;

Here’s a tutorial on saving a game through the documentation, it is very robust though.

2 Likes

Thanks! i will try now, and thanks for tutorial i was watching tutorial on youtube but it was for godot 4 and doesn’t worked.

Right, you are using Godot 3, make sure to not tag your posts with godot-4

func save_highscore(highscore: float) -> void:
	var file = File.new()
	var err = file.open("user://savefile.dat", File.WRITE)
	if err == OK:
		file.store_float(highscore)
		file.close()
	else:
		push_warning("Could not open file: " + str(err))


func load_highscore(default_value: float = 0.0) -> float:
	var file = File.new()
	var err = file.open("user://savefile.dat", File.READ)
	if err == OK:
		return file.get_float()
	else:
		# Could not open, might not exist
		return default_value;
1 Like

Smth went wrong, there is no error but still don’t save my score, maybe this is the problem ?

extends Label
	
func _physics_process(delta):
	var timer_text = get_node("../Timer1").text
	self.text = "High Score: " + timer_text

I’m guessing you did not call the functions, they are not overrides like _physics_process or _ready, you have to decide where and when to save or load the highscore.

okay, i want to save high score when player dies

:confused: have created somthing like this

extends Label

var time_alive: float = 0.0
var running: bool = true
var highscore = 0

func _ready():
	highscore = load_highscore()
	$Highscore.text = str(highscore)
	$Player.connect("death_signal", self, "_on_Player_death")
	text = str(int(time_alive))
	# Start processing every frame
	set_process(true)

func _on_Player_death():
    # Get the current score from Timer1 label
    var current_score = $Timer1.text.to_int()
    if current_score > highscore:
        # Update the high score if the current score is higher
        highscore = current_score
        save_highscore(highscore)
        $Highscore.text = str(highscore)
		
func _process(delta):
	if running:
		# Increment the time_alive by the time passed since the last frame
		time_alive += delta
		# Update the Label's text to show the elapsed time in seconds
		text = str(int(time_alive))

func save_highscore(highscore: float) -> void:
	var file = File.new()
	var err = file.open("user://savefile.dat", File.WRITE)
	if err == OK:
		file.store_float(highscore)
		file.close()
	else:
		push_warning(err)
		
func load_highscore(default_value: float = 0.0) -> float:
	var file = File.new()
	var err = file.open("user://savefile.dat", File.READ)
	if err == OK:
		return file.get_float()
	else:
		# Could not open, might not exist
		return default_value;
func stop_timer():
	running = false

but it didnt work.:confused:

It looks superb! I think the last step to is check if the high score is actually higher, it will be easier if we remove the script from the high score label and update it in this _process

func _process(delta):
	if running:
		# Increment the time_alive by the time passed since the last frame
		time_alive += delta
		# Update the Label's text to show the elapsed time in seconds
		text = str(int(time_alive))

		# Check if high score needs to be updated
		if time_alive > highscore:
			$Highscore.text = str(int(time_alive))
1 Like

But how its super :sweat_smile:there is an error


before putting your code

This is a Timer.gd error, where is that script attached to? Does it have a child “HighScore” and “Player”?

I will send screenshot

Seems like that stuff would be better put on the Game node’s script. That way it can find $Highscore, $Player, and $Timer1 with ease

You mean timer1 script put in game gd yes?

I think most of it, yes

1 Like


and now this error

What text did this refer to previously? $Timer1.text right?