0 votes

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.

in Engine by (32 points)

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

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?)

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.

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.

1 Answer

+1 vote
Best answer

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 ;)

by (360 points)
selected by

Noooooooo! That's exactly what i was looking for!!! :-D Fantastic

What if i need to disable certain nodes just before the new frame is rendered?
Because:
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 framepredraw() as i read in docs,or ?
if you have done it already,wich is the best approach?

Thank you so much!

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

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)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.