GroveGames.BehaviourTree 0.4.9

dotnet add package GroveGames.BehaviourTree --version 0.4.9                
NuGet\Install-Package GroveGames.BehaviourTree -Version 0.4.9                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GroveGames.BehaviourTree" Version="0.4.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GroveGames.BehaviourTree --version 0.4.9                
#r "nuget: GroveGames.BehaviourTree, 0.4.9"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GroveGames.BehaviourTree as a Cake Addin
#addin nuget:?package=GroveGames.BehaviourTree&version=0.4.9

// Install GroveGames.BehaviourTree as a Cake Tool
#tool nuget:?package=GroveGames.BehaviourTree&version=0.4.9                

GroveGames Behaviour Tree

Build Status Tests Latest Release NuGet

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

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(defendSequence));
    }
}

Debugging

To assist with testing and debugging behavior trees, GroveGames Behaviour Tree includes a Visual Debugger that helps track the current state of each node in real-time.

  1. Enable Visual Debugger: To use the debugger, ensure your behavior tree class derives from GodotBehaviourTree. After instantiating your behavior tree, call the SetRoot method and pass in a new Root node to initialize the tree structure. Additionally, remember to call the Enable method to activate debugging. Here’s an example:

    public override void _Ready()
    {
        _characterBT = new CharacterBT();
        _characterBT.SetRoot(new Root(new Blackboard()));
        _characterBT.SetupTree();
        _characterBT.Enable();
        AddChild(_characterBT);
    }
    
  2. Node State Tracking: Once enabled, you can observe states such as Running, Success, or Failure for each node. This is especially useful for visualizing sequences, conditions, and actions that may fail or succeed based on game conditions.

  3. Godot Integration: With debugging enabled, information about node states will be displayed under the Debugger tab, allowing you to track the AI behavior tree’s state updates frame-by-frame.

Example Visual Tree

Below is an example of how a behavior tree appears in the visual debugger, showing nodes and their states during runtime.

Example Visual Tree

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
        }
    }
}

Customization

Extend the framework with new functionality by creating custom nodes:

  • Action Nodes: Inherit from Node and implement specific behaviors in Evaluate.
  • Decorator Nodes: Inherit from Decorator and modify the behavior of a single child node.
  • Composite Nodes: Inherit from Composite and 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:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push the branch (git push origin feature/your-feature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.9 443 1/22/2025
0.4.8 168 1/21/2025
0.4.7 429 1/10/2025
0.4.6 107 12/26/2024
0.4.5 241 12/11/2024
0.4.4 102 12/10/2024
0.4.3 105 12/10/2024
0.4.2 436 11/21/2024
0.4.1 99 11/21/2024
0.4.0 115 11/14/2024
0.3.10 153 11/12/2024
0.3.9 115 11/12/2024
0.3.8 105 11/12/2024
0.3.7 109 11/11/2024
0.3.6 112 11/8/2024
0.3.5 98 11/7/2024
0.3.4 95 11/7/2024
0.3.3 99 11/6/2024
0.3.2 96 11/6/2024
0.3.1 99 11/6/2024
0.3.0 99 11/6/2024
0.2.2 98 11/5/2024
0.2.1 99 11/5/2024
0.2.0 110 11/5/2024
0.1.9 101 11/5/2024
0.1.8 106 11/4/2024
0.1.7 104 11/4/2024
0.1.6 103 11/4/2024
0.1.5 102 11/4/2024
0.1.4 104 11/4/2024
0.1.3 102 11/4/2024
0.1.2 103 11/4/2024
0.1.1 106 11/4/2024
0.1.0 110 11/3/2024
0.0.4 112 11/1/2024
0.0.3 108 11/1/2024
0.0.1 105 11/1/2024