Don't know how to use ScreenTree create_timer

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

I tried from documentation example

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

func timeout():
print("Working")
pass

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

ramazan | 2022-01-12 10:20

:bust_in_silhouette: Reply From: sporx13

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()