Best way to handle a lot of NPCs?

Hey everyone, I need some guidance on NPC controlling.
My game is about a few months old and I’ve felt like I’ve tried everything I know (or could come up with) to handle our NPCs in-game. It’s a tycoon game, similar to Theme Park tycoon’s NPC system and like Retail Tycoon.

I’ve tried multiple approaches.

My first approach was to create a co-routine for every NPC to run on, this was by far the easiest to program however, the server was just about dead after a few hours running.

Next, I tried a different approach, I had one game loop running on Heartbeat (denounced to 2 seconds) that would loop through every NPC in the game and send events to them such as, move to here, find this, do this. This way was super performant! However, it was extremely difficult to debug, maintain, and overall program in this way. My NPCs would end up jumping around, doing wild things I didn’t know could even happen.

If this information is needed, I use ROBLOX pathfinding, it serves me well, I have tried other methods such as A* with little to worse performance changes.

Has anyone had experience with these kinds of systems and could give me some advice on how to improve?
Thanks!

2 Likes

Certain things can be passed over to the client. You know how Retail Tycoon has a slider for where the NPCs are handled? Running some of the NPCs on the client can alleviate this stress. If you haven’t already, a master-slave architecture should significantly reduce the stress on the server, by grouping NPCs together. Lua is not multithreaded, which makes this issue significantly harder. Hardest but most clear solution is simply revising your NPC AI. Expensive calculations should be reduced or even removed. There isn’t a simple way around this, I’m afraid.

2 Likes

You can use Object Oriented programming or collection service.

Blockquote Certain things can be passed over to the client. You know how Retail Tycoon has a slider for where the NPCs are handled? Running some of the NPCs on the client can alleviate this stress. If you haven’t already, a master-slave architecture should significantly reduce the stress on the server, by grouping NPCs together. Lua is not multithreaded, which makes this issue significantly harder. Hardest but most clear solution is simply revising your NPC AI. Expensive calculations should be reduced or even removed. There isn’t a simple way around this, I’m afraid.

Sounds interesting with NPC grouping. I have all my NPCs render on the client for sure. I’ll give it a try and see what bounces back.

Blockquote You can use Object Oriented programming or collection service.

I use OOP and Collection service to render on the client.