Behavior Trees or 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: they provide lots of flexibility, are very powerful, and they are really easy to make changes to.
Lets first look at the first advantage: flexibility. With a FSM, how do you run two different states at once? The only way is to create two separate FSMs. With a behavior tree all that you need to do is add the parallel task and you are done – all child tasks will be running in parallel. With Behavior Designer, those child tasks could be a PlayMaker FSM and those FSMs will be running in parallel.
One more example of flexibility is the task guard task. In this example you have two different tasks that play a sound effect. The two different tasks are in two different branches of the behavior tree so they do not know about each other and could potentially play the sound effect at the same time. You don’t want this to happen because it doesn’t sound good. In this situation you can add a semaphore task (called a Task Guard in Behavior Designer) and it will only allow one sound effect to play at a time. When the first sound finishes playing the second one will start playing.
Another advantage of behavior trees are that they are powerful. That isn’t to say that FSMs aren’t powerful, it is just that they are powerful in different ways. In our view behavior trees allow your AI to react to current game state easier than finite state machines do. It is easier to create a behavior tree that will react to all sorts of situations whereas it would take a lot of states and transitions with a finite state machine in order to have similar AI. In order to achieve the same results with a FSM you would end up with a spaghetti state machine.
One final behavior tree advantage is that they are really easy to make changes to. One of the reasons behavior trees became so popular is because they are easy to create with a visual editor. If you want to change the state execution order with a FSM you have to change the transitions between states. With a behavior tree, all you have to do is drag the task. You don’t have to worry about transitions. Also, 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 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.