How to stop nodes from getting processed when they are not used?

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

I am coming from a game framework background where i have complete visibility over my main loop so i was trying to get a better understanding of how godot works.

I placed a lot of static bodies, no sprite, just static bodies with a rectangle collision shape to test the impact on performance. For my tests i did not collide with them, just observed.

I noticed even if the static bodies dont have a component that needs to get rendered they still get processed.
Having a lot of static bodies inside of the camera area made a big impact on framerate.
Having them outside of the camera area improved framerate but the framerate was still significantly impacted by their existence.

So it seems that when inside the camera some heavier process is happening, and even if they are not inside the bounds of the camera, nodes still get processed. What is actually happening in these cases?
Is a process function called at all times regardless of having a script attatched?

How would one go about having many nodes and not have them be processed if outside of the camera bounds? To be abie to keep them stored in memory when not needed, because the only way around this that i see is to destroy them and re-instance them when required again.

Same behavior happened when placing lots of sprites, of course when visible within the camera bounds the performance hit was the most significant, but even if they exist outside of the camera bounds it seems that there is a performance hit.

Im not familiar with c++ to dive into the source code and start investigating, maybe there is some explanation/guide that i have missed.
I hope im making sense. Thanks!

:bust_in_silhouette: Reply From: klaas

any node that is in the tree gets somewhat “processed”.
PhysicsBodies are checked for collisions and get the forces applyed.
Renderable nodes get transformed and checked against the camera frustum.
So even they are in the camera view or not they have to be handled in some way.
Every script in the tree attacht to some signal or processing callback is executed.

If you want your nodes to be not “processed” you must hide/disable them.

You can hide() renderable nodes or set there visible attribute to false.
You can disable the CollisionShapes with disabled = true
You can stop script processing with set_process(false) or set_physics_process ( false )

Check your Debugger->Monitore tab for performance analysis

I hope this helps

yes this was very helpful i did not know about that, thanks a lot!
I found that even if nodes are hidden/disabled/set_process(false) they must still at least be iterated over though the scene tree to be checked. This lead me to discover that it is also possible to remove a scene/node from the scene tree with remove_child() without it being destroyed, which could be an alternative.
Thanks a lot!

Mejisto | 2020-07-26 23:24

You can set the processed mode of the node/tree to say the node should not be processed while the game is or is not paused. For example:

func _ready(): process_mode = Node.PROCESS_MODE_WHEN_PAUSED

Gatada | 2022-10-12 11:54