- Notifications
You must be signed in to change notification settings - Fork19
Behavior Tree implementation for the Godot Engine as an addon in pure GDScript
License
godot-addons/godot-behavior-tree-plugin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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/behv_godot), which is itself based on ideas/concepts fromquabug/godot_behavior_tree.
- Clone this repository into your
res://addonsor use git submodule. - In your project settings, enable the plugin
- Add a BehaviorTree and a Blackboard node to a scene
- Grab references to the blackboard and behavior tree (use an exported NodePath and then call get_node() on it to get the actual node
- In
_processor_fixed_processcall the tree'stick(actor, ctx)passing in the actor and the blackboard instance - In your actions, remove the default action script, make a new one, have it
extends "res://addons/godot-behavior-tree-plugin/action.gd"and add a tick method where all of your logic will go
- Easy to use and reason about - just Behavior Trees!
- Unobtrusive - to integrate an existing workflow
- the BT primitives are implemented as Nodes that get added directly onto a scene; Actions are just normal nodes that you extend with a script
- Build out a tree and attach it to your actor/entity sprite and just call
.tick()during_process()or_fixed_process() - Uses a simple
Dictionaryvalue as the context object
- Add add little as possible to Godot -
- The system uses existing numeric constants (
OK,FAILEDandERR_BUSY) as stand-ins for the existing "Success, Failure, Running" concepts in BT. - The entire thing is implemented as a pure-GDScript addon that can be easily added to a project without having to rebuild the Godot editor
- The system uses existing numeric constants (
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. TheBehaviorTree node accepts only asingle child node. For example, you would probably add some kind of composite such as aBehaviorSequence node.
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'stick(tick) function recursively.
TheTick object 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).
TheBlackboard acts as a memory repository for your actor. It is passed in to the tree, and itsget() andset() functions will store things based on arguments given. For example, passingget() a key, a reference to the tree (contained on theTick object), and a reference to the node, a node can pull node-specific information. Leaving off the node reference will automatically make theget() search only the tree level storage. Usingset() works similiarly. Callingset() 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 aBlackboard, as it is stored on aTick object.
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 isOK orFAILED. Notice that, when a child returnsERR_BUSY orBehaviorError, the composite node must return the state immediately. All composite nodes are represented graphically as a white box with a certain symbol inside.
ABehaviorSequence node runs a collection of child nodes, stopping at the first failure orERR_BUSY.
TheBehaviorSequence node ticks its children sequentially until one of them returnsFAILED,ERR_BUSY orBehaviorError. If all children return the success state, the sequence also returnsOK.
- Will return and complete if any child returns
FAILED, returningFAILED - Will return
OKif all children returnOK - Will return if any child returns
ERR_BUSY. Will resume at theERR_BUSY-returning child
ABehaviorSelector node runs a collection of child nodes, stopping at the first success orERR_BUSY.
TheBehaviorSelector node ticks its children sequentially until one of them returnsOK,ERR_BUSY orFAILED. If all children return the failure state, the priority also returnsFAILED.
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.
- Will return and complete if any child returns
OK, returningOK - Will return
FAILEDif all children returnFAILED - Will return if any child returns
ERR_BUSY. Will resume at theERR_BUSY-returning child
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.
TheBehaviorFailer decorator is the inverse ofBehaviorSuceeder, this decorator returnFAILED for any child result.
TheBehaviorInverter decorator negates the result of its child node, i.e.,OK state becomesFAILED, andFAILED becomesOK. Notice that, inverter does not changeERR_BUSY orBehaviorError states.
TheBehaviorLimiter decorator 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.
TheBehaviorMaxTime decorator 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.
TheBehaviorRepeater decorator sends the tick signal to its child every time that its child returns aOK orFAILED value, or when this decorator receives the tick. Additionally, a maximum number of repetition can be provided.
TheBehaviorRepeatUntilFailed decoroat keeps calling its child until the child returns aFAILED value. When this happen, the decorator return aOK state.
Similar to theBehaviorRepeatUntilFailed decorator, theBehaviorRepeatUntilSucceed decorator calls the child until it returns aOK.
TheBehaviorSucceeder is a decorator that returnsOK always, 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.
BehaviorAction nodes 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 returnsOK if it could be completed; returnsFAILED if, for any reason, it could not be finished; or returnsERR_BUSY while executing the action.
BehaviorCondition nodes 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 returnOK if the condition has been met andFAILED otherwise. Notice that, conditions do not returnERR_BUSY nor change values of system.
- https://github.com/Kriet108/godot-behavior-tree-plugin
- https://github.com/brandonlamb/godot-behavior-tree-plugin
- https://github.com/olsonjeffery/behv_godot
- https://github.com/quabug/godot_behavior_tree
- http://blog.renatopp.com/2014/07/25/an-introduction-to-behavior-trees-part-1/
- http://blog.renatopp.com/2014/08/10/an-introduction-to-behavior-trees-part-2/
- http://blog.renatopp.com/2014/08/10/an-introduction-to-behavior-trees-part-3/
About
Behavior Tree implementation for the Godot Engine as an addon in pure GDScript
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.