Animationtree: Cheking if animation is finished playing

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

HI everyone, I currently have these lines of code:

if hit == true:
	state_machine.travel("death")
	queue_free()

My objective is to finish the animation of a certain node before removing it from a parent node. My problem is, state_machine.travel(“death”), and queue_free() is being executed simultaneously. Is there a way to determine if the animationtree’s finished playing the animation? Thanks

:bust_in_silhouette: Reply From: JulioYagami

You can do that by using yield keyword:

if hit == true:
    state_machine.travel("death")
    yield(your_animation_node, animation_finished_signal_name)
    queue_free()

yield stops executing the function until the given signal is emitted. If the signal returns some value, var value = yield(args) can be used to get it.

Okay thanks. I will look into it.

ddarkmoto | 2020-01-14 07:07

Read more about yield here: Coroutines with yield

JulioYagami | 2020-01-14 19:55

You mean the AnimationPlayer, used by AnimationTree? Nope, it doesn’t return any signals when used in an AnimationTree. And AnimationTree doesn’t have signals for that.
Also is_playing() doesn’t work in AnimationTree, since it always returns TRUE.
So i think there are a lot of issues to be fixed in AnimationTree.

Migoun | 2020-04-02 16:56

:bust_in_silhouette: Reply From: pox

Kinda late but I found a solution, It’s a bit bothersome but it works

You can use a “Call Method Track” in every animation
Add a key at the end, select a method with a string parameter and write the animation name in the key “Args” section to pass it

I got it from this reddit post: Reddit - Dive into anything

Since I only needed it for a few animations it wasn’t that bad, but between this and other common issues like only having boolean conditionals or not being able to edit the animation speed directly without a blend node… it really screams the need for an update of the animation tree

Also, a simple solution for your problem would be to directly choose the queue_free() method in the call method track (search it at the top)

Hello everyone!

I’m kind of new in GODOT and I had this same issue and could find a direct method. But I found a way around the issue that is kind of easy to implement (at least for me).

For StateMachines in AnimationTree exists a function that could get the current time of the animation (get_current_play_position). When played, this value should be the same or increased its value. But when the animation ends, its value goes directly to 0.

My walk around needs a global variable saving the prior value of the play, and a function that compares the prior value to the current animation value.

I leave you my code. Hope this could be helpful :slight_smile:

var prior_play_position:float = 0.0

func Get_Is_Playing():
	var temp:AnimationNodeStateMachinePlayback = animation_tree.get("parameters/playback")
	var current_play_position:float = temp.get_current_play_position()
	if prior_play_position > current_play_position:
		prior_play_position = current_play_position
		return false
	else:
		prior_play_position = current_play_position
		return true

Please keep in mind that I use this for knowing when to change this animation. When an animation is changed, the I initialize the prior value to 0.