Using function returns object instead of return value.

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

I have code something like this:

node1:

func _ready():
     if return_hello_world_after_a_second() == "Hello, World":
     print("Success!")

func return_hello_world_after_a_second():
      return yield(node2, "signalHello")

node2:

var t = Timer.new()
signal signalHello(text)

func _ready():
   t.set_one_shot(true)
   t.set_wait_time(1)
   t.start()
   yield(t, "timeout")
   signalHello(text)

but instead it prints something like: [GDScriptFunctionState:1479]
How do i make it print hello, world?

note: also, this is sample code. The code in the game is essentially the same, but for a separate system.

:bust_in_silhouette: Reply From: Lopy

When you yield with a yield(t, “timeout”) the calling function receives a GDScriptFunctionState. This object represents the function as it was interrupted. You can use yield(return_hello_world_after_a_second(), "completed") to wait for the actual return.

If a function does not always yield, or may yield multiple times, you get:
var result = return_hello_world_after_a_second()
while result is GDScriptFunctionState:
. result = yield(return_hello_world_after_a_second(), "completed")

Note that those yielding behaviors are likely to change somewhat in Godot 4.

Thanks for answering my question! It helped a lot!

IonizedPro45 | 2021-01-05 15:27