Remove/Add Nodes every frame (remove off-screen nodes)

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

In my 2d game there is a map with many Nodes (let’s say they could be 150/200 or more for very large maps),and the camera can move dynamically anywhere in the map at any time (it can jump in any area at any frame).

What i need to do is to draw only nodes that are inside the screen area(camera) and disable all that are off-screen,but i need to keep them in memory(just disable their draw and their execution or godot processing them).
How can i do it?

I’ve read many approach and methods but i am still not sure wich one to use.

My idea is to just remove() them from the scenetree and attach the newones before the frame is rendered,but how can i do it? Wich is the best approach?

I will be very thankful if someone can show me a correct approach to use and i will test it.

Thank you in advance.

Is setting their visibility to false not good enough for what you are trying to accomplish?

bloodsign | 2021-02-11 23:04

My actual approach is:
when the camera move to a new position i am removing nodes wich are not inside the screen and add the new ones. But it’s not in sync with the frame redraw.

I guess setting visibility to false will be a better approach,but godot will still process those nodes? (i mean calling their callbacks etc?)

StefanoGrossiNature | 2021-02-12 10:10

well, dynamically removing and adding nodes, depending on the size of those nodes might cause some small lag spikes?
I’m not entirely sure myself. My thinking is, wouldn’t it be better to keep them in memory rather than destroying them?
I would suggest to use the [debugger]. On [Monitor] Tab, you can check items on the list such as FPS, Memory, that is being used when the game is running.

You can see how much impact removing and adding your nodes does, and compare it if you are just setting nodes visibility to false.

bloodsign | 2021-02-12 11:42

My approach is not to unload them from memory but just removing them from the SceneTree (and then adding them again),this is actually not causing a lag (but i have to test it in a better way,so i am not sure).
My actual issue is:
1- how to sync it with the frame rendering (i need to add/remove them just BEFORE the new frame render)
2- Leaving them allocated will cause godot to still process them (cpu) ? Maybe signals…callbacks…some loops…etc?

If i solve this i am done.

Hope now it’s more clear.

StefanoGrossiNature | 2021-02-12 12:09

:bust_in_silhouette: Reply From: Surtarso

setting visibility to false wont “stop” the nodes, they will keep processing like they were visible, but will be invisible only.

you want to add a VISIBILITY ENABLER node to the nodes you want disabled and set its properties to stop all processes from the nodes (pause animations, freeze bodies, pause particles etc etc), that way, anything that is not on camera will be “paused” in every aspect until it is on camera its area again

no coding involved :wink:

Noooooooo! That’s exactly what i was looking for!!! :smiley: Fantastic

What if i need to disable certain nodes just before the new frame is rendered?

  1. the camera move to a new position
  2. the new frame rendered will need to have the nodes in the camera area enabled,and everything outside camera area disabled
    So I should use the VisualServer signal frame_pre_draw() as i read in docs,or ?
    if you have done it already,wich is the best approach?

Thank you so much!

StefanoGrossiNature | 2021-02-16 14:48

Im assuming you camera MOVES

just set the visibilityenabler node AREA bigger so it starts 1 frame before it enters camera

the size of the visibility enabler area will be the one that the camera needs to “see”

if it gets “teleported” somewhere IDK… but the visibility enabler should kick the same frame the camera sees it I imagine, try it out

Surtarso | 2021-02-16 15:02

you can also try messing with the visibility notifier property of the visibility enabler node by code to size and position where you need them (in or out of camera)

Surtarso | 2021-02-16 15:13

Fantastic,that’s what i was looking for.
I will try it out and tune it,but this is for sure the right approach to use.

Thank you so much!

StefanoGrossiNature | 2021-02-16 16:44