0 votes

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.

Godot version 3.2
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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.

by (2,484 points)
edited by

Thanks for answering my question! It helped a lot!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.