Behavior Trees vs Finite State Machines
In what situations do you use a behavior tree over a finite state machine (such as Playmaker)? At the highest level, behavior trees are used for AI while finite state machines (FSMs) are used for more general visual programming. While you can use behavior trees for general visual programming and finite state machines for AI, this is not what each tool was designed to do.
Behavior trees have a few advantages over FSMs for use within AI:
- Implicit Transitions
- Responsive
- Fast Iterations
- Parallelism
Implicit Transitions
Behavior trees transition between states implicitly, meaning the transition doesn’t need to be explicitly set. Lets take a look at the tree from the What is a Behavior Tree page:
Notice there is no direct transition between the Stacked Conditional task and the Stacked Action task. Instead it is up to the Sequence task to determine how to transition from one child task to another. This makes adding new tasks to the behavior tree extremely easy as you can just connect it and you’re done.
Responsive
Classic behavior tree implementations traverse each task in depth first search order every frame in order to determine which task should be active. This makes the behavior tree responsive to any changes in that a new task will be selected anytime a new set of conditions are valid. While this makes the tree responsive, it’s not very performance friendly to traverse the entire tree every frame. Behavior Designer Pro implements a concept called Conditional Aborts which allows only select conditional task to reevaluate every frame. If the status of the conditional task changes then the behavior tree will respond based on those changes. This makes the behavior tree responsive while also having it be performance friendly.
In order to active the same level of responsiveness with FSMs you would need to explicitly set a transition from every state to every other state. This quickly turns into a spaghetti mess and is unmaintainable.
An example of a spaghetti state machine
Faster Iterations
This relates to the implicit transitions, but iterating on your behavior tree is extremely quick due to not having to worry about the connections between tasks. In the above example if you want to play an animation after the agent has attacked you just need to ensure the new task is connected to the parent Sequence task. There are no conditions that need to be set in order to ensure the task gets called. In addition, it is really easy to completely change how the AI reacts to different situations just by changing the tasks around or adding a new parent task to a branch of tasks. With a FSM you need to ensure the transition conditions are correct in order for that state to activate at the right time.
Parallelism
Standard behavior tree implementations contain a Parallel task allowing your tree to run multiple tasks at once. This isn’t possible with standard FSM implementations. The only way is to create two separate FSMs which obviously is not a good solution.
With that said, behavior trees and FSMs don’t have to be mutually exclusive. Behavior trees can describe the flow of the AI while the FSM describes the function. This combination gives you the power of behavior trees while still having the functionality of FSMs.