0 votes

does not work

func ready():
while true:
print("Hello World")

but this can print
func _ready():
while true:
print("Hello World")
yield(gettree(), "idle_frame") #This idle frame
in Engine by (49 points)

If you just want an update loop. add set_process(true) inside _ready() and then define a function called func _process(delta):
It will be called every frame with delta being the interval in seconds since the previous call

2 Answers

+1 vote
Best answer

It's because it is using yield(). This returns out of the current function, returning control to the program. That way everything else can execute. It only returns to the code once the signal or resume() is applied. Then it carries on where it left off.

In this example idle_frame is a signal that fires after a game frame has passed.

The first example your code is trapped in that loop and locks the program.

In the second example, the yield is going to yield for one frame. Allowing the program to continue. Then once the idle_frame is signaled, the code will resume at that point, go up into the while loop, and do it again, yield again. So your infinite loop should only execute once per frame.

However, nothing below that while true: will ever execute, because the function is trapped in that loop.

by (5,203 points)
selected by

forgot to comment :D
Thank You for explaining a noob like me

No problem. ^_^

The yield() stuff can be hard to track if you're unfamiliar with where one flows to and out from.

0 votes
# Do stuff
# Wait one frame
yield(get_tree(), "idle_frame")
# Do stuff
by (211 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.