GroveGames.BehaviourTree
0.3.6
See the version list below for details.
dotnet add package GroveGames.BehaviourTree --version 0.3.6
NuGet\Install-Package GroveGames.BehaviourTree -Version 0.3.6
<PackageReference Include="GroveGames.BehaviourTree" Version="0.3.6" />
<PackageVersion Include="GroveGames.BehaviourTree" Version="0.3.6" />
<PackageReference Include="GroveGames.BehaviourTree" />
paket add GroveGames.BehaviourTree --version 0.3.6
#r "nuget: GroveGames.BehaviourTree, 0.3.6"
#:package GroveGames.BehaviourTree@0.3.6
#addin nuget:?package=GroveGames.BehaviourTree&version=0.3.6
#tool nuget:?package=GroveGames.BehaviourTree&version=0.3.6
GroveGames Behaviour Tree
A modular and extensible behavior tree framework for AI development in C# for the .NET and Godot Engine. This system allows for the creation of complex AI behaviors by combining various Node, Composite, and Decorator components.
Table of Contents
- Overview
- Features
- Installation
- Getting Started
- Usage Example in Godot
- Customization
- Contributing
- License
Overview
This behavior tree framework enables AI agents to make decisions and perform actions based on conditions in a dynamic environment. Use this setup to build intelligent game characters with modular and reusable nodes.
Features
- Modular Nodes: Includes
Selector,Sequence,Decorator, and custom action nodes. - Blackboard System: A shared data space for AI agents to store and retrieve contextual information.
- Godot Integration: Works seamlessly within Godot using the node structure.
- Extensibility: Easily add new node types and custom behaviors.
Installation
Install the package via .NET CLI:
dotnet add package GroveGames.BehaviourTree
For Godot:
dotnet add package GroveGames.BehaviourTree.Godot
Getting Started
Creating a Behavior Tree
To set up a behavior tree, create a class that inherits from Tree and override the SetupTree method to define the AI structure.
Example Diagram
graph TD
Root(Root) --> Selector
Selector --> AttackSequence(Sequence: Attack Sequence)
Selector --> DefendSequence(Sequence: Defend Sequence)
AttackSequence --> Condition1[Condition: IsEnemyVisible == true]
AttackSequence --> Cooldown1[Cooldown]
Cooldown1 --> Repeater1[Repeater]
Repeater1 --> Attack[Attack]
DefendSequence --> Condition2[Condition: IsUnderAttack == true]
DefendSequence --> Cooldown2[Cooldown]
Cooldown2 --> Repeater2[Repeater]
Repeater2 --> Defend[Defend]
Example Nodes
Attack
public class Attack : Node
{
public Attack(IParent parent) : base(parent) {}
public override NodeState Evaluate(float delta)
{
Console.WriteLine("Attacking");
return NodeState.Running;
}
}
Defend
public class Defend : Node
{
public Defend(IParent parent) : base(parent) {}
public override NodeState Evaluate(float delta)
{
Console.WriteLine("Defending");
return NodeState.Running;
}
}
Example Tree
Here's an example of a CharacterBT class that builds an AI behavior tree:
public class CharacterBT : Tree
{
public CharacterBT(GroveGames.BehaviourTree.Nodes.Root root) : base(root) { }
public override void SetupTree()
{
var selector = Root.Selector();
var attackSequence = selector.Sequence();
attackSequence.Attach(new Condition(() => IsEnemyVisible()));
attackSequence
.Cooldown(1f)
.Repeater(RepeatMode.UntilSuccess)
.Attach(new Attack(attackSequence));
var defendSequence = selector.Sequence()
defendSequence.Attach(new Condition(() => IsUnderAttack()));
defendSequence
.Cooldown(1f)
.Repeater(RepeatMode.UntilSuccess)
.Attach(new Defend(defenceSequence));
}
}
Usage Example in Godot
Below is a full example of setting up and using the behavior tree in a Godot scene:
public partial class Character : Godot.Node
{
private CharacterBT _characterBT;
public override void _Ready()
{
_characterBT = new CharacterBT(new Root(new Blackboard()));
_characterBT.SetupTree();
}
public override void _Process(double delta)
{
_characterBT.Tick((float)delta);
}
public override void _Input(InputEvent @event)
{
if (@event.IsPressed())
{
_characterBT.Abort(); // Aborts the current tree
}
}
}
Explanation
- Ready Method: Initializes the behavior tree and assigns entities (e.g., player and enemy).
- Process Method: Ticks the behavior tree every frame to update actions based on elapsed time.
- Input Method: Allows manual interruption of the behavior tree, useful for testing or player-triggered events.
Customization
Extend the framework with new functionality by creating custom nodes:
- Action Nodes: Inherit from
Nodeand implement specific behaviors inEvaluate. - Decorator Nodes: Inherit from
Decoratorand modify the behavior of a single child node. - Composite Nodes: Inherit from
Compositeand define logic for multiple child nodes. - Blackboard: Use the blackboard to share data between nodes. For example, store target information or flags.
Example: Custom Decorator (Delayer)
This Delayer decorator delays the execution of a child node by a specified amount of time:
public class Delayer : Decorator
{
private readonly float _waitTime;
private float _interval;
public Delayer(IParent parent, float waitTime) : base(parent)
{
_waitTime = waitTime;
}
public override NodeState Evaluate(float deltaTime)
{
_interval += deltaTime;
if (_interval >= _waitTime)
{
_interval = 0f;
return base.Evaluate(deltaTime);
}
else
{
return NodeState.Running;
}
}
public override void Reset()
{
_interval = 0f;
}
}
This decorator only allows the child node to execute once the specified wait time has passed.
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -am 'Add new feature'). - Push the branch (
git push origin feature/your-feature). - Open a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GroveGames.BehaviourTree:
| Package | Downloads |
|---|---|
|
GroveGames.BehaviourTree.Godot
A lightweight behaviour tree framework developed by Grove Games for .NET and Godot |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.4.10 | 146 | 9/12/2025 |
| 0.4.9 | 764 | 1/22/2025 |
| 0.4.8 | 217 | 1/21/2025 |
| 0.4.7 | 499 | 1/10/2025 |
| 0.4.6 | 166 | 12/26/2024 |
| 0.4.5 | 285 | 12/11/2024 |
| 0.4.4 | 153 | 12/10/2024 |
| 0.4.3 | 153 | 12/10/2024 |
| 0.4.2 | 490 | 11/21/2024 |
| 0.4.1 | 135 | 11/21/2024 |
| 0.4.0 | 158 | 11/14/2024 |
| 0.3.10 | 208 | 11/12/2024 |
| 0.3.9 | 170 | 11/12/2024 |
| 0.3.8 | 158 | 11/12/2024 |
| 0.3.7 | 163 | 11/11/2024 |
| 0.3.6 | 153 | 11/8/2024 |
| 0.3.5 | 155 | 11/7/2024 |
| 0.3.4 | 139 | 11/7/2024 |
| 0.3.3 | 155 | 11/6/2024 |
| 0.3.2 | 147 | 11/6/2024 |
| 0.3.1 | 152 | 11/6/2024 |
| 0.3.0 | 154 | 11/6/2024 |
| 0.2.2 | 164 | 11/5/2024 |
| 0.2.1 | 144 | 11/5/2024 |
| 0.2.0 | 168 | 11/5/2024 |
| 0.1.9 | 149 | 11/5/2024 |
| 0.1.8 | 160 | 11/4/2024 |
| 0.1.7 | 151 | 11/4/2024 |
| 0.1.6 | 143 | 11/4/2024 |
| 0.1.5 | 157 | 11/4/2024 |
| 0.1.4 | 156 | 11/4/2024 |
| 0.1.3 | 164 | 11/4/2024 |
| 0.1.2 | 151 | 11/4/2024 |
| 0.1.1 | 161 | 11/4/2024 |
| 0.1.0 | 152 | 11/3/2024 |
| 0.0.4 | 166 | 11/1/2024 |
| 0.0.3 | 159 | 11/1/2024 |
| 0.0.1 | 162 | 11/1/2024 |