I am programming this game that spawns waves of enemies. The wave consisted of ca. 20 enemies. Sprites were relatively small. I use rectangle collision shape. No collisions at spawn and also the seconds after.
Sprites are all preloaded at the start of the game.
Since I am unexperienced, I am wondering if the issue is that those 20 enemies are being spawned in the same frame? The profiler shows that the corresponding script takes most of the time. This script contains multiple functions to determine which enemies, how many and at which position they should be spawned. On the other hand I would be dissapointed if Godot can´t handle 20 enemies.
In result I have an ugly lag every time a wave is spawned. Is my assumption right, that the problem is that I am doing all those “simple” calculations + instancing + adding the instances to the scene in the same frame?
Thank you for trying to help. The root cause was indeed that I spawned too many instances in the same frame. I am working now with away and call_deferred as with smaller waves of enemies.
Don’t think about it like that. Godot can easily process 20 of your enemies. But instantiation is an expensive operation that allocates memory or even reads from disk. And you have to fit that into 16ms (at 60fps), that’s why it’s a struggle
You can easily spawn one enemy per frame (or even multiple, probably). Just set up a variable with how many enemies is still left to spawn and create a couple of them each _process. If they come from around the screen, the player will never know
This has a hidden benefit. If your enemies have a timer on them, all timers would fire at once, which could cause a stutter. By spawning them one by one, you spread this into a longer time span too!
I get that approach now. My problem was that I created a stupid architecture. I want to spawn waves of enemies that have to keep a certain order and distances to each other. I thought it would be easier to spawn multiple of those waves as one big wave to have a shorter code.
Now, I have a reworked system with up to 6 instantiations per frame.
But besides reducing the number of instances spawned per frame I also run the preparation of the wave_pool, the instantiation of the wave_pool and adding those instances to the main_scene on separated frames. 120 fps run like butter haha.