In a previous chapter, we saw a basic but effective way to implement and manage character states and behaviors: Finite State Machines (FSMs). FSMs are simple to implement and very intuitive, but they have a fatal flaw: it is very hard to make them scale once states and transitions start getting numerous. For example, imagine a character that behaves differently depending on the amount of health and mana it has (high, medium, and low); we have a state when both health and mana are high, one in which, health is medium and, mana high, one in which they are both medium, and so on. In total, we have nine states just for that. If we add other conditions (such as player proximity, time of day, equipment, player’s score, or whatever you may imagine), the number of states grows exponentially.
Luckily, we have a solution: Behavior Trees (BTs). In essence, Behavior...