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:
-
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.
-
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:
- Use the rendering server instead of the node and scene system. But using it could make the game harder to compile out?
- 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 =)