Last Updated: March 02, 2016
·
2.836K
· moechofe

Yield always ⚠ return from the function in Godot

Coroutines & Signal are a powerfull feature for the Godot game engine. It's remind me the lua coroutines. I use it a lot until I was stuck with a problem.

yield(node_instance, signal_name)

The line above stop the execution of the current function and wait for node_instance to send the signal_name with the instruction:

node_instance.emit_signal(signal_name)

One day, I put the yield instruction inside a sort of a getter function that use the return keyword. It was without counting the fact that yield return from the function. It actually return an instance of a GDFunctionState used to resume the function.

# The getter
func get_thing():
    a_node.call_deferred("execute")
    return yield(a_node,"executed")

# In the a_node script:
signal executed(val)
func execute():
    OS.delay_msecs(100)
    emit_signal("executed",123)

The example above do not work: get_thing will return a GDFunctionState instead of 123.