Is there a more elegant way to pause an async method until an animation has finished playing?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dostoi
	while (animationPlayer.IsPlaying())
	{
		await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
	}

It basically reset a very small timer many times, so the script is blocked until the animation has finished playing. It works fine, although I’ve noticed that this part of the script loops each time the animationPlayer “isPlaying()” anything else. It shouldn’t add a big load of stuff in memory until the async method is done, but it’s not very elegant.

Does anyone know a better way to pause an async method until an animation has finished playing?

Thank you for your time.

Have you tried using the animation_finished() signal?

Ertain | 2022-06-05 14:17

Thank you for your answer. I did. Unfortunately, in some configuration, signals can’t be intercepted (or it would require to build things differently). Have a great day.

Dostoi | 2022-06-07 15:48

:bust_in_silhouette: Reply From: godot_dev_

When your animationPlayer finishes playing, you could have it emit a signal , for example “finished”. Then you could use subroutine to simulate a blocking function as follows:

if animationPlayer.IsPlaying():
   #block until finished
    yield(animationPlayer,"finished")
   #here your code will reach it when your animation finishes

Although I would recommend using a signal handler function instead, as using yields can get messy. Maybe something like follows:

animationPlayer.connect("finished",yourSignalHandlerScript,"_on_handle_animation_finish")