Why is a timer ruining my code even though it worked just fine without a timer?

Godot Version

I am using Godot version 4.4

Question

I’m trying to add a respawn mechanic to my game, but whenever I add a timer to the code, it all falls apart, and I have no idea why.

Here’s the original code (without a timer):


@onready var player: CharacterBody2D = $Player
@onready var health_bar: ProgressBar = $CanvasLayer/HealthBar
@onready var checkpoint_1: Node2D = $Checkpoints/Checkpoint1
@onready var checkpoint_2: Node2D = $Checkpoints/Checkpoint2
@onready var death_wait: Timer = $"Timers/Death wait timer"

func _on_killzone__level_1_area_entered(_area: Area2D) -> void:
	player.health = player.health - 15
	player.animated_sprite.play("damage")
	player.can_dash = true
	player.health_bar.health = player.health
	player.Damtime.start()
	player.is_taking_damage = true
	player.gravity = 0
	player.large_dust.emitting = true
	player.large_dust.emitting = false
#Checkpoints expire when they de-activate, making it unable to activate again

	#Death script
	if player.health <= 0 and checkpoint_1.activated and not checkpoint_2.activated:
		print("player died")
		player.position = checkpoint_1.position
	elif checkpoint_2.activated:
		checkpoint_1.activated = false
	
	if player.health <= 0 and checkpoint_2.activated and not checkpoint_1.activated:
		print("player died")
		player.position = checkpoint_2.position
	elif checkpoint_1.activated:
		checkpoint_2.activated = false

Here’s the final code (with a timer):


@onready var player: CharacterBody2D = $Player
@onready var health_bar: ProgressBar = $CanvasLayer/HealthBar
@onready var checkpoint_1: Node2D = $Checkpoints/Checkpoint1
@onready var checkpoint_2: Node2D = $Checkpoints/Checkpoint2
@onready var death_wait: Timer = $"Timers/Death wait timer"

func _on_killzone__level_1_area_entered(_area: Area2D) -> void:
	player.health = player.health - 15
	player.animated_sprite.play("damage")
	player.can_dash = true
	player.health_bar.health = player.health
	player.Damtime.start()
	player.is_taking_damage = true
	player.gravity = 0
	player.large_dust.emitting = true
	player.large_dust.emitting = false
#Checkpoints expire when they de-activate, making it unable to activate again

func _on_death_wait_timer_timeout() -> void:
	#Death script
	if player.health <= 0 and checkpoint_1.activated and not checkpoint_2.activated:
		print("player died")
		player.position = checkpoint_1.position
	elif checkpoint_2.activated:
		checkpoint_1.activated = false
	
	if player.health <= 0 and checkpoint_2.activated and not checkpoint_1.activated:
		print("player died")
		player.position = checkpoint_2.position
	elif checkpoint_1.activated:
		checkpoint_2.activated = false

how does it fall apart? What do you expect to happens versus what really happens?

Probably because you never start the timer, so it never times out.

2 Likes

Thanks gertkeno and dragonforge-dev!

The game still works, but the respawn mechanic doesn’t work.
What I hoped for, was when the player dies, after about half a second, they’d teleport to the nearest activated checkpoint

I tried starting the timer in the code, but when the health is zero, the player just bobs up and down on the spikes really fast but when I dash up, I go back to the checkpoint. After that, whenever I hit the spikes, I bob up and down, and the dashing thing doesn’t work.

Is bobbing up and down re-entering the kill zone? That would restart the timer before it could finish, maybe you can check timer.is_stopped() before starting it.

2 Likes

Thanks a TON gertkeno! It worked PERFECTLY!