Wait for AnimationTree finished animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rebl

I am using AnimationPlayer and AninmationState and want to play a “die” animation, and THEN queue_free() but I cant make it work. My first guess was to use “yield”, but I dont know how:

if (health <= 0):
   state_machine.travel("Die")
   # yield...
   queue_free()

My second guess was to wait for AnimationPlayer to emit a signal, but it does not emit anything:

func _on_AnimationPlayer_animation_finished(anim_name):
	print(anim_name)
	if (anim_name === "Die"):
		queue_free()
:bust_in_silhouette: Reply From: Inces

Yeah, I had that problem lately too. I hope new Godot will finally provide animation_finished signals for animationTree. What I did to get around it was getting animation.length from animationplayer and yielding

yield(get_tree().create_timer(animationlength),"timeout")

But this sollution is not perfect. Yield has another flaw in Godot 3.2, it processess in pause. So someone might cheese your game by pausing on animations :slight_smile:

:bust_in_silhouette: Reply From: jobel

Maybe this is not the best way to achieve it, but you can call a function when a specific animation finished by calling a method in the last frame in the AnimationPlayer node.
When the AnimationTree play’s the animation in the last frame, the function will be called.

I used C#, but the idea is the same, here are the steps in images:


step 2
Choose the node with your script attached, in my case is the Player’s node.

in my case I’m using c#, but the idea is the same, create a function to call at the end of an animation.

After writing our function to call, then we go back to Godot editor and at the last frame we insert the key, but this case in the Player Functions’ Track
inserting the function key

We’re done! Test it and I hope you like it. One more thing, we can use the same method for different animations, just insert the new track in the other animations, insert the key in the last frame and select your method/function.