Help with enemy death handling

Godot 4.2.1

I’m wondering what would be the best way to handle enemies death. My question is mostly aimed at the animation and waiting for the animation to end before queue_free(). For now my solution is based on knowing the length of time the animation needs to finish. Is there any other way to do this? Thank you for your help in advance!
Here is my function:

func deal_with_damage():
	if global.playerAttacked and !global.playerAttackCooldown:
		enemyHealth -= 20
		global.playerAttackCooldown = true
		$CooldownPlayerAttack.start()
		print(enemyHealth)
		if enemyHealth <=0:
			enemyHealth = 0
			isDead = true
			animation.play("Death")
			await get_tree().create_timer(1).timeout
			queue_free()

change to this:

func deal_with_damage():
	if global.playerAttacked and !global.playerAttackCooldown:
		enemyHealth -= 20
		global.playerAttackCooldown = true
		$CooldownPlayerAttack.start()
		print(enemyHealth)
		if enemyHealth <=0:
			enemyHealth = 0
			isDead = true
			animation.play("Death")
			await animation.animation_finished
			queue_free()

assuming your animation is AnimationPlayer, and it doesnt loop

2 Likes

thank you very much!