Death animation not playing (2D platformer)

Godot Version

v4.2.1

Hello everyone!
I am currently trying to make a death animation play upon entering an enemy body, which has a signal attached to its hitbox. Everything else seems to work fine. When my player touches the enemy, the function _on_hitbox_body_entered(body) is being called, which then initialises var dead to true and calls the death() function to reload the current scene.

However, the animation itself is not played. I tried adding a boolean if dead == false at the beginning of the process function with the idea to stop any movement and animations once the enemy has been touched, so that the death animation can play, unfortunately to no prevail. I’m really curious to know, where the logic error is, so any tips and help would be greatly appreciated!

Thanks in advance!

Here is the code:

extends CharacterBody2D
 
@export var SPEED : int = 80
@export var JUMP_FORCE : int = -250

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var dead = false
 
func _physics_process(delta):
	
	if dead == false:
		
		var direction = Input.get_axis("left","right")
		
		if Input.is_action_pressed("run") and is_on_floor():
			SPEED = 110.0
		if Input.is_action_just_released("run"):
			SPEED = 80.0
		
		if direction:
			
			velocity.x = SPEED * direction
			
			if is_on_floor():
				
				$AnimatedSprite2D.play("run")
			
		else:
			
			velocity.x = move_toward(velocity.x, 0, SPEED)
			
			if is_on_floor():
				
				$AnimatedSprite2D.play("idle")
		
		# Rotate
		
		if direction == 1:
			$AnimatedSprite2D.flip_h = false
		elif direction == -1:
			$AnimatedSprite2D.flip_h = true
			
		
		# Gravity
		
		if not is_on_floor():
			
			velocity.y += gravity * delta 
			
			if velocity.y > 0:
				
				$AnimatedSprite2D.play("fall")
				
		if Input.is_action_just_released("jump") and velocity.y < 0:
			velocity.y = JUMP_FORCE / 4
		
		# Jump
		
		if is_on_floor():
			
			if Input.is_action_just_pressed("jump"):
				
				velocity.y = JUMP_FORCE
				$AnimatedSprite2D.play("jump")
		
			
		move_and_slide()
			
func death():
	$AnimatedSprite2D.play("dead")
	print("you died")		
	get_tree().reload_current_scene()
       dead = false

		

func _on_hitbox_body_entered(body):
	if body.is_in_group("Enemy"):
		dead = true
		death()

(Please format your code using the </> button on the edit toolbar.)

It seems to me that the problem is simply that you’re calling get_tree().reload_current_scene(). This pretty much just resets everything immediately, including all variables you might have set.

You probably want to only call this function after the death animation has finished playing, or perhaps after awaiting some timer.