I’m making a top down 2D game with a large world and I have npcs going about their own daily routines. For a big map and a large amount of npcs, how should i approach this in terms of performance?
Here is what I have:
NPCs are characterbodies2D each with their own:
Sprites (different body parts for customization)
Animation Players (one for fx and one for body animations)
Collisions
Statemachine nodes.
Performance notes with 100 npcs in an empty scene. Some are outside of camera rendering area: (I don’t have a very strong pc) (each test is independent from the other)
Performance normally without npcs: 400 fps
Performance with 100 npcs: 75 fps
Performance with 100 npcs with collisions disabled: 85 fps
Performance with all npcs invisible: 100 fps
performance with 100 npcs minus the two animation players: 230 fps
performance with physics process disabled and statemachine halted: 290 fps
Performance with a script that does all of the above: 400 fps in areas with no npcs, gradually decreases with more npcs on screen until it reaches 90 fps
Half the problem is solved. I can now run my game without encountering problems if the npcs are widely spread apart. But I still plan on having certain parts with many of them like a battlefield…etc. How would i have 100 npcs on my screen without the fps dropping? if it even possible.
The last part of the problem, obviously, the npcs outside camera are disabled, but i want them to still go on about their own routine in the background outside of player interference. How am i supposed to approach this?
Disclaimer: this idea might be absolut trash. Please let me know so I can learn as well
Everything outside camera skips physics updates, e.g to just run it once per second (1 fps). Count the amount of skipped physics_update (most likely sum up skipped delta time) and multiple the new positions with that to catch up on the missing frames. Actions like attacking can then obviously only execute every second as well which could be a downside
Though… This could also lead to fps drops every second
Thanks for taking the time to reply.
1 and 2 i’m already doing but for 3, i still want faraway npcs to do stuff so things change dynamically without player’s interference. I thought perhaps I could just have an npc manager that simulates faraway npcs interactions then when player is near it would draw and calculate them based on the simulation result but i wonder if there was a more efficient way to do it.
I think the idea is right, but i think you somehow want to remember how long theyve not been updated and then calculate what happened based on the time theyve not been updated
I added a “last_met” float to each npc and had those far away parented to a “Simulation” node which increases the value of each of its children’s “last_met” variable in its own process function.
Thanks for your help guys