Adding and removings hundred of objects, potentially per frame; good or bad?

Godot Version

4.2.2

Question

I have a bullet hell game wherein hundreds of projectiles(area2D that I move manually by changing its position per frame) might be active at any point, and maybe twenty or thirty will be deleted (and spawn short-term particle systems) per few tenths of seconds.

Is this sustainable, especially if I’m considering adding lights to each projectile?

Are there any weird behaviors? Will the Node Name be so long that Godot just crashes? (i.e @Area2D@37812718923869 or similar taking up more space than Godot has set aside for the name property)

This should be possible, but you might want to consider pooling the objects. Dont queue_free them when they are done, instead remove them from the scene tree and reuse them later

1 Like

Adding and Removing nodes is one of the most taxing operations in godot, avoid doing it per-frame in general, let alone 1000 times per-frame.

Here’s a old video on exactly what you are trying to do, object pooling will be your first task. instead of queue_free, hide and set_process(false). instead of .instantiate(), show and set_process(true)

2 Likes

How would I do that in code? Removing them from the scene tree sounds very much like queue_free()ing them.

Are you referring to object pooling?

Yes object pooling. You can remove them by calling “remove_child(node_to_remove)” on the parent or just use hide() and set_process(false) as gertkeno suggested. Make sure to save them in an array or something to prevent memory loss

I’m still confused though; what happens if I remove_child() instead of queue_free()? Won’t I need to instantiate it again to bring the object back?

Sorry for the bother, but I have a question:

Is it best to make the pool of objects in _ready() or do it in the editor? I have 50-ish shooter objects and I reckon each will need 4 or 5 projectiles pooled.

I think there is no reason to do that manually. Actually, it’ll still be instantiated by the engine when the game is launched.

However, you’ll only get at most 300 projectiles instantiated as you described. I think Godot can handle that since the implementation is highly optimized with C++, rather than the crazy garbage collector in C#.

The difference is that queue_free deletes the object from memory while remove_child only removes it from the scenes tree, but it can be added to it again with add_child without instantiating again

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.