Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Behavior Tree implementation for the Godot Engine as an addon in pure GDScript

License

NotificationsYou must be signed in to change notification settings

godot-addons/godot-behavior-tree-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 

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.

Installation

  1. Clone this repository into yourres://addons or use git submodule.
  2. In your project settings, enable the plugin
  3. Add a BehaviorTree and a Blackboard node to a scene
  4. Grab references to the blackboard and behavior tree (use an exported NodePath and then call get_node() on it to get the actual node
  5. In_process or_fixed_process call the tree'stick(actor, ctx) passing in the actor and the blackboard instance
  6. In your actions, remove the default action script, make a new one, have itextends "res://addons/godot-behavior-tree-plugin/action.gd" and add a tick method where all of your logic will go

Design philosophy

  • 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 simpleDictionary value as the context object
  • Add add little as possible to Godot -
    • The system uses existing numeric constants (OK,FAILED andERR_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

State Values

  • 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.

Node types

BehaviorTree

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.

Tick object

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).

BehaviorBlackboard

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 Types

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.

BehaviorSequence

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 returnsFAILED, returningFAILED
  • Will returnOK if all children returnOK
  • Will return if any child returnsERR_BUSY. Will resume at theERR_BUSY-returning child

BehaviorSelector

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 returnsOK, returningOK
  • Will returnFAILED if all children returnFAILED
  • Will return if any child returnsERR_BUSY. Will resume at theERR_BUSY-returning child

Decorator Types

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.

Failer

TheBehaviorFailer decorator is the inverse ofBehaviorSuceeder, this decorator returnFAILED for any child result.

Inverter

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.

Limiter

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.

Max Time

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.

Repeater

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.

Repeat Until Failed

TheBehaviorRepeatUntilFailed decoroat keeps calling its child until the child returns aFAILED value. When this happen, the decorator return aOK state.

Repeat Until Succeed

Similar to theBehaviorRepeatUntilFailed decorator, theBehaviorRepeatUntilSucceed decorator calls the child until it returns aOK.

Succeeder

TheBehaviorSucceeder is a decorator that returnsOK always, no matter what its child returns. This is specially useful for debug and test purposes.

Leaf Types

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.

Action

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.

Condition

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.

Links

About

Behavior Tree implementation for the Godot Engine as an addon in pure GDScript

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp