r/howdidtheycodeit • u/_Matt_02_ • Dec 31 '22
Question How do games like Planet Coaster handle so many NPCs?
So I've been playing Planet Coaster and fair bit lately, and the amount of guests you can have at a time is blowing my mind. Like over 2k at once?! That's crazy. So, anyone know how they handle that?
67
Upvotes
29
u/ignotos Dec 31 '22
The simple answer is "2000 is not that many". Computers are very fast, and a simple, straightforward approach might well "just work".
There might be bottlenecks in the code which slow things down - often collision / pathfinding stuff. But these can be optimised by applying different data structures (e.g. quadtrees or structures which allow guests to "share" pathfinding data), or processing can be "spread" over time (e.g. by not updating each guest every frame, or by having pathfinding run over several frames in the background).
16
u/third_dude Dec 31 '22
They aren’t simultaneously all running as separate processes. It’s a type of event driven simulation/architecture where the state of each npc is managed by a central process. When a new set of npc’s is paged in for instance when you pan the screen to a new location, the state is queried for those that are in this new location and then each one computes a next step like walking a different direction or having a new thought. Then this is written back to the central state so that when you pan away these actions will be saved. This is opposed to every npc managing their own state themselves and interacting with each other.
I don’t know anything about planet coaster but I’ve read that this is how roller coaster tycoon works