One of the primary goals of game AI is finding a simple and scalable solution for editing logic. Finite state machines have the advantage of being quite simple, but for large systems you’ll need a hierarchical FSM to provide reusable transitions between states.
Such HFSMs are certainly a popular way for making scalable logic. However, they do not provide any modularity for states; you can’t reuse states to provide logic for different goals or situations without rewiring them. Behavior trees (BT) on the other hand take a different approach…
States or Behaviors
Behavior trees focus on increasing the modularity of states by encapsulating logic transparently, for example by using nested states. Certain hybrid HFSM implementations provide this feature also.
Figure: A tree made of modular behaviors.
The secret is to remove the transitions to external states, so the states become self-contained. In fact, once you do that, they’re not really “states” anymore because they don’t have transitions; they’re just behaviors, or a collection of actions that execute and terminate.
A Programming Analogy
You may ask, since the transitions are no longer encoded explicitly in each state, how is it possible to sequence behaviors using a behavior tree?
This is done using the same method as most programming languages — which is demonstrably more scalable. In C++, Python or any other modern language, each operation does not contain a pointer to its successor, as it would be difficult to reuse these operations in different contexts (like calling them from another function).
Instead, operations are inserted into a parent scope (e.g. in a function, or for a conditional check), and they are executed according to the semantics of the parent scope. So in essence, the transitions between the operations are automatically defined based on which type of parent scope is chosen.
Why Behavior Trees Work
If you make your BT implementation extensible (so any type of scope, or composite behavior can be added), it becomes as powerful as any programming language. But at the same time, BTs remain very simple and usable when you provide standard composites such as sequences and selectors.
In fact, you can rebuild most state machine using only sequences and selectors. Then adding custom decorators into the mix you get something extremely powerful yet still very intuitive.
Ultimately, it is possible to translate a simple BT of sequences and selectors to a FSM if necessary. The FSM does you more control over the individual transitions, but the BT make the logic modular and easier to reuse.