Behavior Trees are exactly what they sound like! A Behavior Tree is considered a tree-based data structure that is used in all sorts of fields today for controlling the behavior of Artificially Intelligent agents. These so-called “Behaviors” can be anything you, as the programmer, can think of. Maybe you want an AI to walk to a certain location on the map and stop if interrupted by the player. Or maybe you the AI to switch between daily routines, such as washing his/her hands, watching TV, talking to others, picking up items, and sleeping. The AI behavior is whatever you define it to be.
More formally, a Behavior Tree (BT) is defined as a tree of hierarchical nodes that control the logical flow of decision making for an artificially intelligent agent, whether that agent be a Robot, a Non-Player Character (NPC) in a video game, or any other autonomous AI entity. Essentially, Behavior Trees offer a way to structure the switching of tasks (or actions) among an AI through the use of control flow and decision making nodes that the programmer specifies. Behavior Trees generally have many different types of nodes, but all nodes in a Behavior Tree tend to have the same core functionality, independent of the implementation.
A core aspect of Behavior Trees is that the leaf nodes of the tree define the actual behaviors that control the AI entity in a game and the branches of the tree contain Utility nodes that control the logical traversal of the tree depending on the behavior best-suited for the given AI’s situation. I like to call the leaf nodes “Behavioral nodes.”
Each node in a Behavior Tree can return different status messages that inform the parent node about whether a certain event or action occurred.
In a basic Behavior Tree, there are three different kinds of status messages:
Success - A node returns a ‘Success’ status message when an action/operation was performed successfully. For example, if an AI is walking to a certain location and finally arrives at its destination, its behavioral leaf node will return success to its parent.
Failure - A node returns a ‘Failure’ status message when an action/operation could NOT be performed successfully. For example, if an AI is walking to a certain location and the player interrupts this AI by either talking to it or running into it, its behavioral leaf node may return failure to its parent.
Running - A node returns a ‘Running’ status message when an action/operation in still in progress and success or failure has not been determined yet. These nodes will continuously be checked again.