So I was wondering what is your approach to AI optimization for NPC not in sight of the player.
Usually people suggests proximity approach, but this is not good since you can have NPC on the lower level just below you but the path to this place might be quite a long one.
Right now I’m thinking about generating grid of occludees and using those objects determine of visibility of characters inside, when those characters have idle behaviour and are are occluded I will disable the whole character.
NPC will be enabled when the region becomes visible.
@neginfinity OK sorry I forgot to write what type of game I’m talking about.
FPS/TPP kind of game with big world, but not exactly open. Just big level.
yes seeing the player is a problem in this case, But I might just do some raycasts from time to time.
What I would like to know what is the current state of art
This completely depends on circumstances - how much of an impact the AI has on game performance, how your level is built, what you can reasonably change about your level, what you can’t cut from gameplay.
For example if you have a sniper game you’ll have a completely different set of variables you’ll be managing compared to a room clearing game.
Completely disabling an out of sight AI and then having it start again as if nothing happened when the player looks at it, that’s something that takes resources (time/money) to make happen. If you don’t need it, no point doing it.
As already mentioned above, it depends on cirmcumstances.
So first thing is to filter NPCs by sectors / regions etc. if any.
For example, can NPC on lower levels shoot to player/other NPCS above and vice versa?
Assuming shooting in the region is valid for any NPC/player, then need filter them by range.
And finally filter by able to shoot, if target is seen. I.e. using raycast.
Now, how often you run specific checks, is up to you. And you can stagger check for different NPC, instead checking them all at once.
But if something is too far to shoot, obviously no need to run raycast checks.
But finally, profile it. Make stress tests, use profiler and validate your concerns.
In the game I’m working on everything is determined by a 2D grid/navmesh I’m using that tracks the player and all enemies, and which checks whether things are in view of the player every iteration of time.
Enemies either move towards the player or simply move into range and shoot depending on their type. And when the player moves out of view some simply resume their behavior while others go to the player’s last location.
If your game has multiple agents acting at their own in background (think background ships in X series), then obviously the npcs are going to be active at all times, except they might not actually spawn any game objects, and they might not be updated at every frame.
If it is a standard RPG mechanics where things are mostly centered around player and/or predefined scenario, then…
When the player is not in the zone, npcs do not exist.
When the player is not within range where he can observe them, they do not exist. (The level itself might be loaded)
When the player approaches the area of possible interaction, they spawn, but could be frozen.
When the player takes active interest, they unfreeze and start operating.
However.
5. If the player engages the NPCs in combat, then active combatants would become persistant, and remain on the map after player leaves, until combat reaches some conclusion. “NPC gave up on pursuit” is one possible conclusion. In this case the npcs will continue trying to pursue player until they decide to give up, then they return to base, write on their ai blackboard “The player is a jerk, shoot on sight”, and then they despawn.
6. If the NPCs follow schedule, then they should follow it even when they do not exist as objects. Meaning either your schedule system should run faux-npc objects in background… meaning not game objects, but C# classes, OR your scheduling system should be able to return where should someone be and where they should be going given any point of time.
As you can see a lot depends on whether you’re trying to implement a living world, or are using standard “movie set” approach with predefined interactions. In a living world at least some NPCs could exist in some form at all times, but they might not be GameObjects, but raw C# classes that spawn their GameObject avatars when necessary.