AI Logic: Where should it go? AI Controller or Behavior Trees?

Until recently, I’ve been putting 100% of my AI logic inside of behavior trees and using a few helper functions within C++.

I looked at the source code for the Strategy Game example and the Shooter Game, and noticed that they put in a majority of their logic into a derived AI Controller class and the behavior tree tasks just make a call to the AI controller functions. Which way is better? What are the pros and cons? How are you guys doing your game AI?

The latter approach is closer to the way I’ve tended to do it. I think of the BT as just the high-level decision maker - under what conditions and when to do something. I always delegate to the AI controller or its components for the how part, which is where most of the core logic is.

1 Like

Okay, slight thread necro. I’ve got more of a philosophical/conceptual design question I’m trying to wrap my head around… and this is more rubber ducking than anything else, so feel free to ignore or contribute.

If a behavior tree is the decision tree which the AI uses to determine behaviors, and the AI controller is supposed to be the ‘brain’ for the AI character… how do you differentiate what goes into the AI controller vs. what goes into the behavior tree? Both ultimately are making decisions, right?

A little easier to answer: If the character is the body for the AI character, then the AI controller is the brain. The brain is a part of the body, so it makes sense for the AI controller to be a component of a character. There’s a slightly nuanced decision which kind of reminds me of the mind-body problem though: If the character gets a “receive damage” event, the body responds to it by reducing hitpoints (or something project specific), but the mind also has to receive that event so that it can respond in some way. Does the character process the damage event, then forward it on to the brain? How does taking damage effect the behavior tree? Where should an ‘aggro’ system belong? My intuition says it goes in the AI controller since its a brain thing rather than a body thing, and if I put it into a body, then player controlled characters would also have an internal aggro system which has no relevance to them because they’re directly controlling the character and using player logic.

How do players do their logic? I think… it’s more of a weighted list of preferable tasks, right? and as the game world changes, the preference for a specific task increases or decreases. Run out of ammo? the preference for getting more ammo becomes very high. Run low on health? the preference for running away goes really high. Get scared? Hiding becomes preferable. So, this sounds like players are using a complex neural network which has weighted values attached to tasks and the game scenario. It seems that a behavior tree could support a neural network type structure in a way, but its really not designed for that. And for a neural network type AI to work, the learning would have to be persistent across play sessions so that weighting becomes learned. I think I’d love to explore this type of AI system, but for the sake of time and having a deadline to ship a game, this may have to be put on hold and I’ll come back to it later.

Aggro either in a component on the AIController or just on the AIController, there is public exposed logic that specific BT tasks/decorators etc can call. I write things as modular as possible with not much logic in the tasks themselves in case I move away from behavior trees.
For damage, my characters all have a damageable component that fire an event to whoever cares that they took damage and from what. The AIController listens to it and acts on it and then may inform the character to play a different animation or move somewhere.