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
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?
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.
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.
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
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))