What is idle_frame in get_tree()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pin
:warning: Old Version Published before Godot 3 was released.

does not work

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

but this can print

func _ready():
    while true:
        print("Hello World")
        yield(get_tree(), "idle_frame") #This idle frame

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

Lisandro Lorea | 2017-11-03 15:32

:bust_in_silhouette: Reply From: avencherus

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.

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

Pin | 2017-11-03 11:20

No problem. :slight_smile:

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

avencherus | 2017-11-03 16:46

:bust_in_silhouette: Reply From: luislodosm
# Do stuff
# Wait one frame
yield(get_tree(), "idle_frame")
# Do stuff