Behavior Trees are a popular tool used in game development and artificial intelligence for controlling the behavior of game characters or agents. A Behavior Tree is a hierarchical structure that organizes the decision-making process of an agent into a tree-like structure. At each node of the tree, an action is performed that either leads to another node in the tree or returns a result.
An action is the basic building block of a Behavior Tree, representing a simple task or behavior that an agent can perform. For example, an action could be to move the character to a specific location, to fire a weapon, or to wait for a certain amount of time. An action can either return a success, failure, or running status, indicating whether the action was successful, failed, or is still in progress.
Sequences and Selectors are two types of composite nodes used to create more complex behavior in a Behavior Tree. A Sequence node performs a series of actions in a specific order, where each action is only executed if the previous action was successful. This can be used to implement preconditions for a certain behavior. For example, a sequence could be used to create a behavior for an agent to attack an enemy, where the preconditions might include the agent being close enough to the enemy and having enough ammunition.
A Selector node performs a set of actions until one of them succeeds. This can be used to implement interrupts or events that change an agent’s behavior in response to the environment. For example, a selector could be used to create a behavior for an agent to flee from danger if it detects an enemy nearby, or to search for a new objective if its current objective is no longer available.
The Behavior Tree is rerun in every frame of the game from top to bottom. This means that the behavior of the agent is constantly evaluated and updated based on the current state of the game. By using Behavior Trees, developers can create complex behaviors for game characters that are responsive to changes in the game environment.
This is in contrast to Finite State Machines (FSMs), where the agent’s behavior is determined by its current state and transitions between states are triggered by specific conditions or events. FSMs are better suited for modeling relatively simple behaviors with well-defined state transitions, while Behavior Trees are better suited for more complex, dynamic behaviors that require more flexible decision-making and are continuously evaluated and updated based on the current state of the game environment.
Behavior Trees in gw2cc #
The gw2cc framework already includes a complete and basic implementation of a Behavior Tree, which can be located in the gdbt/ folder. To construct a tree structure, the system already in place in Godot is used, specifically the node and scene tree systems. Actions, conditions, and composite nodes are all simple Godot nodes that can be easily arranged in the editor to form a Behavior Tree. The root of the tree must be a gdbt/BehaviorTree.gd node. By using packed scenes, more complex behaviors can be abstracted and made reusable, which enables a highly modular top-down approach to bot AI.