Why updating highscore after pressing retry button?

Godot Version

3

My highscore works fine but the problem is that it only updates after pressing my retry button
highscoremanager.gd(autoload)

extends Node

var high_score = 0

func _ready():
	load_high_score()

func save_high_score(score: int):
	if score > high_score:
		high_score = score
		var file = File.new()
		file.open("user://high_score.save", File.WRITE)
		file.store_var(high_score)
		file.close()

func load_high_score():
	var file = File.new()
	if file.file_exists("user://high_score.save"):
		file.open("user://high_score.save", File.READ)
		high_score = file.get_var()
		file.close()
	else:
		high_score = 0

Game.gd

extends Node2D

var score = 0
onready var high_score_label = $HighScoreLabel
onready var score_label = $Timer # Make sure to set the path to your score label node
onready var area = $deatharea
var is_dead = false

func _ready():
	var yarn_mouse = $YarnMouse  # Adjust the path to your YarnMouse node
	yarn_mouse.connect("score_increment", self, "_on_score_increment")
	area.connect("death", self, "_on_death") 
	update_labels()
	
	

func _on_score_increment():
	if not is_dead:
		score += 1
		score_label.text = str(score)
		print("score is added")
	else:
		print("not scoring bro")
	

func _on_death():
	is_dead = true
	$AnimationPlayer.play("FadeOut")
	print("you died")
	yield(get_tree().create_timer(0.5), "timeout")
	HighScoreManager.save_high_score(score)
	$YarnMouse.queue_free() 



func _on_Button_pressed():
	get_tree().change_scene("res://Scenes/Menu.tscn")

func update_labels():
	score_label.text = str(score)
	var high_score_label = $HighScoreLabel # Adjust the path to your high score label node
	high_score_label.text = "High Score: " + str(HighScoreManager.high_score)

Any ideas???

What do you mean by “only updates after pressing retry”?

When in my game i score for example 3 and my highscore is at that moment is 2 after dying its not changing it but when i click retry button and then going to the menu and starts again at that moment its 3.( I think i explain messy,cuz at this moment i am not at my computer so i cant show a video)

just call update_labels-method in the _on_death()-method:

func _on_death():
	is_dead = true
	$AnimationPlayer.play("FadeOut")
	print("you died")
	yield(get_tree().create_timer(0.5), "timeout")
	HighScoreManager.save_high_score(score)
    update_labels()
	$YarnMouse.queue_free() 

Thanks i will try soon

Thanks thats was the problem,Thank you :+1: :+1: :+1:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.