Spawning with c++ causes a ton of lag

Hi,

I have a game where I spawn in a bunch of actors in specific places (based on a changing database so I can’t put them in manually) on begin play. I have been using some BP scripts to do this but I wanted to switch this code over to c++. When I use c++ to spawn the actors now there is a ton of lag when playing. The spawning code happens at begin play and then is over, so I am totally lost as to what is causing it to drop in quality so dramatically. Any help or ideas is appreciated!

Thanks for replying, I tried that and it still isn’t working. Even if I spawn only 10 actors each second it is still incredibly slow during play. Is there anything different between c++ spawning and blueprint spawning that could cause the issue?

even though i’d want to say cpp is faster, the overhead is minimal afaik.

it might well be an issue with loading the assets, and their size on disk and having to set them up on the gpu.

try spawning empty actors and see if you notice any difference.

otherwise you might try spawning things on different threads but i don’t recall atm how to do it safely. feel free to google around.

another possible optimization is find a way to load things that are relevant first. e.g. if you’re using world partition there’s something that can help you by setting the loading grid properly.

if you don’t use world partition you can create your own solution. in my game for example, i only load the areas during chapter transitions, and only loads the area you’ll be playing in.

if you use pcg you have the same grid for generation and newer versions have the “hierarchical generation.”

there are also LODS that might help. and mipmaps. and virtual texture. all those things could impact loading. how to know? you need to read the docs and run the profiler, maybe the “statistics” panel. there are great videos from epic in youtube about hitches and loads.

make sure your lods are properly set, that your meshes are not bloated (even with nanite there is a discard % or by error, in nanite AND the fallback mesh (separately)).

also think about using object pools i’ve made a generic one. it could help. as you can force spawn them at the start of the game, but keep them hidden until you actually need them. you have to keep an eye on memory, i wouldn’t recommend “preloading” much. but i do recommend pooling. Making sure you're not a bot!

if the engine takes times loading/spawning an asset, it does not matter (much) how you do it, it will still take long time to load.

so you’ll have to focus an what are you loading, maybe why are you loading it.