Optimisation issue with instantiation

Godot Version

4.2

Question

Dear Godot community,

I have an optimization issue that I tried to solve unsuccessfully for some months now…

So a brief concept of the game:
2D survival game in a dynamic living ecosystem. So for this game, I need the different life forms to keep active even when outside the screen.

Currently, I have an FPS drop when instantiating and deleting life entities, which are a specific scene added or removing in the main_scene.

How it works:
Each entity has a timer, at each timeout, they do stuff such as cloning themselves, aka “instantiate a new entity”.
After a while there is too many entities and the FPS drop.

What I did so far:

  1. I test which part of the process was taking the most CPU and it is not the number of entities but it is well the instantiation call that takes a lot of time by frame.

  2. Instead of all individuals calling the instantiation function, I collect all new_instances in a list where I have more control over how to process it.

a) I try to run it on a thread and use deferred_call to add_child in the main scene. But I feel the deferred call is also making an FPS drop. Is it true? Maybe I didn’t use it correctly as I am a newbie with thread.

b) I tried to create in advance a specific number of instances. Then modify it and make them visible only when needed. It works pretty okay but when I have them all in use and need them to change, it causes also an FPS drop.

c) Split the instantiation in batch with a specific timer. Explanation: each X time I instantiate only Y life entities. So far, it works nicely but after some time I still have an FPS drop. With ideal X and Y number I can well decrease the ms use in process Time in the profiler, but the physic time is still high.
Is it possible that instantiation and add_child get slower if there is already a lot of instance in the main scene? If yes what can be a solution to outcome this?

What I didn’t yet try:

  1. Use the rendering server instead of the node and scene system. But using it could make the game harder to compile out?
  2. Use complicated matrices and run all of them on GPU. But I feel I will still have the instantiation issue when I will need to show them in the game.

So happy to hear your suggestion =)

tbh the creation and deletion of objects takes the biggest performance blow, especially in games that is required to draw a lot of objects (such as bullet hells or survivors games)
The biggest performance boost that you can do is pooling the objects in order to handle their life cycle with better grace.

I am sadly not understanding your type of game yet, if they need to be active themselves outside the screen or if they just need to be able to located outside the screen.

Thanks for the reply.

I spend sometime to switch my code with pooling system and… the game runs ways smoother now!

Now I need to figure the best ways to build the pool without causing the game to freeze too much. For now I built the pool by batch separate in time. but still some work to do there.

they need to be active outside the screen and different entity need to be able to interact between them even when player doesn’t notice.