In my game there is a kill counter that tracks when a zombie is ran over, however upon the character death the UI restarts at 0 but upon killing a zombie the counter goes right back to where it was before death. i “borrowed” the kill counting code from https://www.youtube.com/watch?v=Rh_8UXjYTn4 i will provide the code for the player death below and the rest of the code will be in that video, any help at all is greatly appreciated as i am very new and have 0 idea what im doing
extends Area2D
@onready var timer: Timer = $Timer
signal killed
func _on_body_entered(body: Node2D) -> void:
print("You Died")
var timer: Timer = Timer.new()
add_child(timer)
timer.one_shot = true
timer.timeout.connect(_on_timer_timeout)
timer.start(0.5)
if body.has_method("die"):
body.die()
func _on_timer_timeout():
get_tree().reload_current_scene()
If the UI shows 0 until it’s updated with the counter, are you only changing the text when a kill is counted? If so, you’ve likely not changed that text from the default 0. In other words, the kills are not actually being reset. Simply set the kill count to 0 when you need to. For example, you might say GlobalHandler.kills = 0, or make a function in the autoload that sets the kills to 0 and then updates the UI.
You could also do it when dying but then there will be other situations like restarting stage, reloading stage, going to main menu through start menu etc that will still keep the kill_count. Easiest way to cover all those situations would be to set to 0 during player spawn.
You could also consider not storing it in an autoload. Typically when you use an autoload it is because you want to store things between scenes. Here, you dont even want to store the count for the duration of the scene.
In the end it doesnt matter much but just thought i would comment on it! Its easy to clutter up your game with things that you dont even need. For now, i would just keep it as it is and not worry about it, but if you add more things to autoload just consider if you actually need to.