0 votes

I have written a simple code.
But the problem is code does node print "waiting has completed" at all but the code works fine

extends Node

signal completed
export var n=3

func _ready() -> void:
    print("waiting for 3 sec")
    yield(counter(n, 2.0), "completed")
    print("waiting has completed")

func counter(n:int, wait:float):
    while n>0:
        yield(get_tree().create_timer(wait), "timeout")
in Engine by (14 points)

Why does the below code does not print "waiting has completed"?

It does for me. Just copied your code into a fresh scene and got (as expected):

waiting for 3 sec
waiting has completed

some time it works but many time doesn't work for me.

1 Answer

+1 vote


counter(n, 2.0)
yield(self, "completed")

in line 7 should work.

Remember that yield expects an object and a signal, so whatever counter() returns will be used as the object in Line 7. This normally wouldn't work (functions that return nothing, return Null), but since counter() also has a yield statement this is what happens:

counter() will return a GDScriptFunctionState the first time it hits yield in there (Line 15). This is what gets passed into the yield inside _ready. So the yield in line 7 will wait for the signal completed of GDScriptFunctionState (which does exist), and not for the completed signal of your own class.

Also notice that using counter(n, 2.0) will wait 3 * 2 = 6 seconds :).

by (1,131 points)
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.