Waiting for a sequence of action in a loop to happen

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

What I want to do seems rather pretty simple: I call a method on each node in a loop. The method may take some time to execute because of animations. In the end, all methods should be executed sequentially.

The innermost method looks a bit like this (it gets called after multiple indirections all doing a bit of logic):

set_process(false)
$Tween.start()
yield($AnimationPlayer, "animation_finished")
set_process(true)

I then call this in a loop

func do_something_for_all():
	for node in get_children():
		var foo = node.move()
		while foo is GDScriptFunctionState && foo.is_valid():
			foo = yield(foo, "completed")

And then call that loop from _process:

var foo = do_something_for_all()
while foo is GDScriptFunctionState && foo.is_valid():
	foo = yield(foo, "completed")

Sadly, this doesn’t work, and I don’t really know why. I tried a lot of different combinations and changed order etc., each leading to different results. Sometimes the animation plays instantly, sometimes all animations play together instead of sequentially and at the moment the execution stops atyield($AnimationPlayer, "animation_finished"), never executing what comes after for some reason.

To sum it up, it’s all weird and confusing.