Behavior Tree implementation for the Godot Engine as an addon in pure GDScript
A Behavior Tree implementation for the Godot Engine, written in pure GDScript.
This project is a Godot Engine addon that adds a collection of nodes to the editor that facilitate the implementation of Behavior Trees. It is released under the terms of the MIT License.
This is a fork from Brandon Lamb (https://github.com/brandonlamb/godot-behavior-tree-plugin) which is a fork from Jeff Olson (https://github.com/olsonjeffery/behvgodot), which is itself based on ideas/concepts from `quabug/godotbehavior_tree`.
res://addonsor use git submodule.
_fixed_processcall the tree's
tick(actor, ctx)passing in the actor and the blackboard instance
extends "res://addons/godot-behavior-tree-plugin/action.gd"and add a tick method where all of your logic will go
Dictionaryvalue as the context object
ERR_BUSY) as stand-ins for the existing "Success, Failure, Running" concepts in BT.
OK: returned when a criterion has been met by a condition node or an action node has been completed successfully;
FAILED: returned when a criterion has not been met by a condition node or an action node could not finish its execution for any reason;
ERR_BUSY: returned when an action node has been initialized but is still waiting the its resolution.
BehaviorError: returned when some unexpected error happened in the tree, probably by a programming error (trying to verify an undefined variable). Its use depends on the final implementation of the leaf nodes.
Place this at the root of your tree at the AI/agent level. The
BehaviorTreenode accepts only a single child node. For example, you would probably add some kind of composite such as a
From a code perspective, this is a very simple node intended to be the root / entry-point to your Behavior tree logic. It creates a 'tick' object and will then simply call down to its child's
Tickobject is created internally by the BehaviorTree, and passed in to each child node. It mostly functions as a way to pass references through the tree (automatically containing a reference to the tree, the blackboard, and the actor the tree is currently acting on).
Blackboardacts as a memory repository for your actor. It is passed in to the tree, and its
set()functions will store things based on arguments given. For example, passing
get()a key, a reference to the tree (contained on the
Tickobject), and a reference to the node, a node can pull node-specific information. Leaving off the node reference will automatically make the
get()search only the tree level storage. Using
set()works similiarly. Calling
set()with a key and a value will set an entry in memory for any user of the blackboard; calling it with a tree reference as well will store it in the memory for only that tree (or anything with a reference to the tree); and with a tree and a node will store it in memory for that node in the specific tree. All nodes will have access to a
Blackboard, as it is stored on a
Composite nodes can have one or more children. The node is responsible to propagate the tick signal to its children, respecting some order. A composite node also must decide which and when to return the state values of its children, when the value is
FAILED. Notice that, when a child returns
BehaviorError, the composite node must return the state immediately. All composite nodes are represented graphically as a white box with a certain symbol inside.
BehaviorSequencenode runs a collection of child nodes, stopping at the first failure or
BehaviorSequencenode ticks its children sequentially until one of them returns
BehaviorError. If all children return the success state, the sequence also returns
OKif all children return
ERR_BUSY. Will resume at the
BehaviorSelectornode runs a collection of child nodes, stopping at the first success or
BehaviorSelectornode ticks its children sequentially until one of them returns
FAILED. If all children return the failure state, the priority also returns
For instance, suppose that a cleaning robot have a behavior to turn itself off. When the robot tries to turn itself off, the first action is performed and the robot tries to get back to its charging dock and turn off all its systems, but if this action fail for some reason (e.g., it could not find the dock) an emergency shutdown will be performed.
FAILEDif all children return
ERR_BUSY. Will resume at the
Decorators are special nodes that can have only a single child. The goal of the decorator is to change the behavior of the child by manipulating the returning value or changing its ticking frequency. For example, a decorator may invert the result state of its child, similar to the NOT operator, or it can repeat the execution of the child for a predefined number of times. The figure below shows an example of the decorator "Repeat 3x", which will execute the action "ring bell" three times before returning a state value.
BehaviorFailerdecorator is the inverse of
BehaviorSuceeder, this decorator return
FAILEDfor any child result.
BehaviorInverterdecorator negates the result of its child node, i.e.,
OK. Notice that, inverter does not change
BehaviorLimiterdecorator imposes a maximum number of calls its child can have within the whole execution of the Behavior Tree, i.e., after a certain number of calls, its child will never be called again.
BehaviorMaxTimedecorator limits the maximum time its child can be running. If the child does not complete its execution before the maximum time, the child task is terminated and a failure is returned, as shown algorithm below.
BehaviorRepeaterdecorator sends the tick signal to its child every time that its child returns a
FAILEDvalue, or when this decorator receives the tick. Additionally, a maximum number of repetition can be provided.
BehaviorRepeatUntilFaileddecoroat keeps calling its child until the child returns a
FAILEDvalue. When this happen, the decorator return a
Similar to the
BehaviorRepeatUntilSucceeddecorator calls the child until it returns a
BehaviorSucceederis a decorator that returns
OKalways, no matter what its child returns. This is specially useful for debug and test purposes.
Leaf nodes are the primitive building blocks of behavior trees. These nodes do not have any children and therefore do not propagate the tick signal. These nodes perform some computation and return a state value. There are two types of leaf nodes (conditions and actions) and are categorized by their responsibility.
BehaviorActionnodes perform computations to change the actor state. The actions implementation depends on the actor type, e.g., the actions of a robot may involve sending motor signals, sending sounds through speakers or turning on lights, while the actions of a NPC may involve executing animations, performing spacial transformations, playing a sound, etc.
Actions may not be only external (i.e, actions that changes the environment as result of changes on the agent), they can be internal too, e.g., registering logs, saving files, changing internal variables, etc.
An action returns
OKif it could be completed; returns
FAILEDif, for any reason, it could not be finished; or returns
ERR_BUSYwhile executing the action.
BehaviorConditionnodes check whether a certain condition has been met or not. In order to accomplish this, the node must have a target variable (e.g. a perception information such as "obstacle distance" or "other agent visibility"; or an internal variable such as "battery level" or "hungry level"; etc.) and a criteria to base the decision (e.g.: "obstacle distance > 100m?" or "battery power < 10%?").
These nodes return
OKif the condition has been met and
FAILEDotherwise. Notice that, conditions do not return
ERR_BUSYnor change values of system.