What's the best approach to check which enemies AI should be put on Standby when not near players (Optimization for RPGs)

  1. What do you want to achieve? Keep it simple and clear!
    I want to make an open world rpg where there is a lot of enemies in the map (1000+ entities). I already made so only the logic is handled in the server (no NPC parts in the server), while the visuals work on the client.
  2. What is the issue? Include screenshots / videos if possible!
    I feel like that’s not enough optimization. What’s the best way to check which NPCs can be put on standby (in order to reduce the 1000+ active entitites to something like 50-100)? I don’t feel like looping every enemies for each player every x amount of second would be optimal.
    Would it be good enough to check if a player moved a minimum amount of studs from its last position, and only if it did then I start a new check?

Use an object cache for enemies and place spawn triggers on your map, when a player enters a trigger use the cache to supply and activate enemies into the trigger or the trigger’s spawn point. When an enemy is killed or no longer active, return it to the cache. Start the cache small and only add to it on demand.

1 Like

I think I wasn’t too clear, but what I want to know is what trigger would be reliable and optimized for this. Like, do I iterate all enemies every once and a while to check If they should be on standby based on distance? Do I use a touched event to check If a player walked past a region and check that particular region?

Track NPCs per region. Activate NPCs only when a player enters a relevant region.
Spatial partitioning combined with event-driven activation rather than brute-force looping.

1 Like

This is interest management.

What you SHOULDN’T do is for each player, for each NPC, check distance.
That becomes players x NPCs

With 20 players and 1,000 NPCs, that’s 20,000 checks per update cycle.

The best approach is trying region-based activation.
Think in terms of zones, not NPCs.

Instead of saying "Is this NPC near a player?"
Do: "Is this zone near a player?"
If yes → activate all NPCs in zone
if no → standby entire zone

One way you can do this:

  1. Divide your map into regions:
    Split the world into square chunks.

  2. Assign NPCs to regions:
    When an NPC spawns, get its position, calculate the corresponding region and store it in a table.
    Each region keeps a list of NPCs inside it. If NPCs move far, update their region when crossing boundaries.

  3. Track player region:
    For each player, calculate their region using the same grid formula, store their current region and only re-check when they enter a new region instead of checking every frame.

  4. Activate only nearby regions
    When a player enters region (x,z), activate that region and neighboring regions (like 3x3 square around it)
    Only those regions’ NPCs run full AI, everything else should be on standby.

  5. Last but not least, standby:

  6. For NPCs in regions where the players aren’t nearby, stop AI, pathfinding, and whatever else. Just pause their logic.

You could even go as far as keeping an active player count for each region, like a counter that checks how many players in a region at once. If region player count goes to 0 → enter standby and vice versa.

I think I might do that, but merging regions with a chunks system. Thanks a lot, I’ll try that route

This is literally what spatial partitioning means. Great idea. :thinking:

I’m aware it’s spatial partitioning. I was explaining the implementation details so it’s usable advice rather than just the concept name, as per your very helpful reply. I don’t think it was really necessary to come back and reply to me just to point out that I was restating your point, especially in a mildly condescending way. I was just helping to clarify.

1 Like

And you did a great job of it. Textbook.
Figure looking it up would be better, maybe not.