I’m developing a simulation with a few thousand AI NPCs. For performance reasons, when they go offscreen, I’ll turn off visibility and animations. I would also like to use a simplified behavior tree when the NPC goes offscreen. I can’t turn the NPCs completely off - their movements and some properties should be always visible on my world map.
But how do I switch between the full behavior and simplified behavior efficiently? And how to implement resuming from the current node? Or should I add the distance checks in every behavior action and then branch inside?
Example: an NPC can enter a cafe to satisfy its hunger. If player is following the NPC, the player should see NPC behaving smartly - ordering food, paying, eating and repeating until it’s not hungry anymore. But if the player is far enough, the NPC should just enter the cafe and just stay there for some time doing nothing and at the end it will have it’s hunger reset to 0 and move on.
But if player happens to enter cafe right in the middle of NPC trying to satisfy its hunger, the NPC AI should jump to one of the states in its “smart tree”, for example, NPC should be moving to his table to eat or moving to the exit after eating or moving to order a meal (could pick randomly or based on some time variable - how long has the NPC been inside the cafe).
How to implement such behavior tree switching efficiently and correctly?