Can someone explain to me about the "idle" meaning and the call_deferred function

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

Can someone tell me about the meaning of “idle” in the documentation. Idle processing mean the _process tick, so i think that idle time must be the next _process call? Am I correct?

So call_deferred is just a queue of methods that will be invoked in the next _process frame? That method seems so magical to me, almost all the guide/examples in the documentation seems use that method to wrap another method to achieve the safeness.

And, if i use godot, should i wrap every method call inside call_deferred to achive the “safety” without worrying about anything? The documentation about call_deferred is so short but its used so widely

:bust_in_silhouette: Reply From: godot_dev_

From what I understand, the _process function called every idle step, and the engine attempts to meet some desired target frames per second, but does not enforce it, so the delta time passed as an argument to _process may not be exactly 1/60 of a second, assuming 60 FPS.

I believe call_deferred is called after (or maybe before, however you want to interpret it) the idle step.

I am no expert, but an example of a reason why call_deferred is safe is because it waits briefly before executing a function call, and for tasks such as collision handling using the area_entered and area_exited signals from Area2Ds, when the collision processing is being done you cannot alter collision properties of areas (for example disabling collision shapes). To disable a collision shape in response to a collision being detected, you must wait and disable the shape during the idle step (which can be accomplished via call_deferred).

So for your question about using call_deferred for everything, just to be safe, I don’t think you have to. If you are doing simple computations (iterating over child nodes or changing property values, for example), you don’t need call_deferred.

IIRC, isn’t collision things are physics related? So it’s more related to the _physic processing than the idle processing, why call_deferred still work in that case. I think the “idle time” in the method’s documentation is not really related to the “idle processing” page also from the docs?

nganhvu | 2023-03-27 22:37