All Questions


Ajinkya steelx

yield(get_tree(), "idle_frame")

What does this `

yield(get_tree(), "idle_frame")

` do ?

1. get_tree()

2. "idle_frame" ?

  • Nathan Lovato replied

    The function get_tree() is a method defined on the Node class if I recall correctly. It returns the SceneTree singleton; every node in your game is part of that scene tree.

    The SceneTree object has a signal named "idle_frame". It emits it when the next game frame starts (when the engine starts to call _process() on every node in your game).

    So if you yield until that signals is emitted, this makes the function pause until the next frame.

    You can use this to "wait for 1 frame". There are many processes in a game engine that are deferred to the end or a specific time during a frame, like physics processing. And so sometimes, you want to wait a bit to safely access an object.

    In those cases, you can use that yield() until the next frame, or use the call_deferred() and set_deferred() methods, that respectively wait for that "idle frame" to call a function or set a property.