How to Properly Animate Character States in Godot 4.2.2 Without Overlapping Animations?

Godot Version

4.2.2

Question

I am new to Godot, and I am trying to animate a character based on its current movement state, such as run, idle, jump, hurt, and death.

I noticed that when I play the hurt animation, the idle animation still plays because it doesn’t wait for the hurt animation to finish. To address this, I added a boolean variable:

bool is_hurt = false

and used a timer like this:

func hurt(dmg: int):
	is_hurt = true
	current_health -= dmg
	$AnimatedSprite2D.play('hurt')
	hurt_timer.start(0.4)
	take_damage.emit(current_health)
	
	if current_health <= 0:
		die()
		

func _on_hurt_timer_timeout():
	is_hurt = false

However, this approach feels a bit off to me because I can’t be sure that the animation will always play for exactly 0.4 seconds. Additionally, if I need to apply this to another animation, I would have to create another boolean variable and a timer.

Is there a “proper” or better way to handle this?

AnimatedSprite2D emits a finished signal that you can connect to.