0 votes

I tried from documentation example

func _physics_process(delta):
yield(get_tree().create_timer(1.0), "timeout")
pass

func timeout():
print("Working")
pass
Godot version 3.4
in Engine by (37 points)

I think:
here __physics_process(delta):
You are constantly creating a timer and the time never ends.

1 Answer

0 votes

First thing. I don't think using yield in physics process like that is good idea. Because it is creating timers every frame. Try adding print("test") after yield. It will wait for 1 sec, and then just go nuts.

What I see is that you think you should create "timeout" function. That is not the case. First argument is Object (in that case timer) and second argument is SIGNAL from that object that yield is waiting for.

Then, what is yield?

yield(object: Object = null, signal: String = "")

is a method that as documentation says:

"Stops the function execution...bla bla until something"

You can resume function by calling

resume()

or you can add object and signal to yield(). That way yield will wait for that object to emit signal. In your case it creates timer and waits for "timeout" signal. It emits after 1 sec, and script resumes.

yield(get_tree().create_timer(1.0), "timeout")

So for example if you paste code that is shown below. You script on ready will create timer, wait 1 sec, emit signal "timeout" and now yield will know that it needs to resume and will do next thing - print "test".

func _ready():
    yield(get_tree().create_timer(1.0), "timeout")
    print("test")

How can you use it?
In many, many ways. for example you can do function that place bomb on ground when you press button. You put yield in that function with a timer and after timeout do explosion, play animation with AnimationPlayer etc.

PS: in godot 4.0 yield() is called await()

by (69 points)
edited by
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.