timer not working/not sending signal on time out

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

here is all the code in the script

const max_health = 100
var health = 100
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	print(str(health))
	
	if health <= 0:
		$CharacterBody3D.hide()
		$respawn_timer.start()


func _on_player_damage(value):
	health -= value


func _on_respawn_timer_timeout():
	health = max_health
	$CharacterBody3D.show()

all the calls are correct i quite sure its some kind of problem with the timer node but im not too sure what, any ideas on how to fix?

:bust_in_silhouette: Reply From: maict

If the calls are working, then is your timer set to one shot? Currently, my assumption is that you’re grabbing the timer respawn_timer from the tree in your scene, so I don’t think that would be the issue there.
enter image description here

The timer node is set too one shot yes

turtilla955 | 2023-04-23 02:03

:bust_in_silhouette: Reply From: AndyCampbell

It looks like the problem is that the timer is re-started every frame when health <=0, so the timer never gets a chance to finish. As long as health <= the timer gets set back to zero again every frame.

Try this:

  1. Create a variable to track if you are respawning, false by default
  2. Ensure your timer has one_shot set to true so it only restarts when you tell it to
  3. Change the test for health <= 0 to also check that respawning is NOT true
  4. Before starting the timer, set respawning to true, so you will not start it again
  5. When the timer times out, set respawning back to false

This might work

const max_health = 100
var health = 100
var respawning = false


func _ready():
    pass # Replace with function body.

func _physics_process(delta):
    if health <= 0 and not respawning:
        respawning = true
        $CharacterBody3D.hide()
        $respawn_timer.start()


func _on_player_damage(value):
    health -= value


func _on_respawn_timer_timeout():
    health = max_health
    $CharacterBody3D.show()
    respawning = false

I didn’t think of this thanks I quite certain that’s this is the problem

turtilla955 | 2023-04-23 02:04

Also will keep this in mind for when I encounter something like this, thank you AndyCampbell.

maict | 2023-04-23 02:06

1 Like