How many lines of code should an NPC execute per tick?

Hi All,

I’m working on a multiplayer project where I might have, at any given time, about 1000 NPCs in game, and up to maybe 100 NPCs on screen (although most often the numbers will be between 0 and 50 NPCs on screen).

I intend for the survival of my NPCs to be dependent on their success in gathering food, making money to buy food, and not getting killed by other characters. I’m also working on a rather complicated memory-management and trust-relationship system for NPC interactions with other NPCs or players. I don’t think I’ll need every NPC to be processed every frame; I’m thinking maybe I can get away with only processing off-screen NPCs 1 time per second, out to 1 time per 2 seconds for very far away NPCs, and on-screen NPCs between 2 and 5 times per second, depending on their proximity to a player.

As I’m writing this code, I’m starting to think that maybe it’s a bit too long for an NPC. Have any of you guys made complicated AIs in the past? How many lines of code should a reasonable AI have to process per tick?

Also, have any of you made AI that had a dynamic working set of memory (like, to remember past interactions)? If so, how much memory did you allocate and why?


Honestly, use no ticks if necessary. What I like to
Here’s some tips:

  • Use AI Perception
  • Use is ‘Timeline’. It has looping and enter connector, meaning it can use an controllable tick.
  • Use custom events when you can to trigger events you need to do.
  • Implement the game mode as much as possible.
  • Save and load info from a save file instead of keeping it in memory via ticks

That’s just some ideas. Try and avoid using ticks as much as possible.
If you need some more tips or help, feel free to PM me.


You’re going to find other issues with performance, like draw calls

Thanks for the responses, guys :slight_smile:

@especially helpful. All tips are welcome! I’ll definitely hit you up sometime in the future if I need more advice on this topic :slight_smile:

@You mean because of the number of actors on screen? Do you have any advice about how to mitigate some of that, so I can keep it in-mind from the early stages of my implementation?

For many actors you need a custom solution, I don’t know that UE4 has anything built in, UE3 had something specifically for that, when they did Gears of War 3 they had some areas where many locusts would be around and they made some kind of batching system for skeletal meshes to reduce the number of draw calls. The Assassin’s Creed games had to make something as well to handle the crowds. If you try to do it without something like that, the performance won’t be good.

@Solid information. I’ll definitely be thinking about that as I work out the graphics in the game. This is my first experience with UE4, and I haven’t gotten very far into the graphical implementation of my game yet, so unfortunately I don’t have many intelligent questions to ask. Can you think of any tutorials or open-source custom solutions (of any kind) that I can look into, just to see what it looks like in practice to override the built-in methods for making draw-calls on actors?

Links to manual pages on this topic are also good!