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