{"id":19878393,"url":"https://github.com/grovegs/behaviourtree","last_synced_at":"2026-02-11T12:11:27.741Z","repository":{"id":260661589,"uuid":"880738831","full_name":"grovegs/BehaviourTree","owner":"grovegs","description":"A modular and extensible behavior tree framework for AI development in C# for the .NET and Godot Engine.","archived":false,"fork":false,"pushed_at":"2025-01-22T15:39:29.000Z","size":251,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T02:09:30.865Z","etag":null,"topics":["ai","behaviour-tree","dotnet","godot"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grovegs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-30T09:10:55.000Z","updated_at":"2025-03-30T06:23:44.000Z","dependencies_parsed_at":"2024-11-01T17:29:19.156Z","dependency_job_id":"0a8107b7-aa86-4a62-876f-d0a05f0b114e","html_url":"https://github.com/grovegs/BehaviourTree","commit_stats":null,"previous_names":["grovegs/behaviourtree"],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grovegs%2FBehaviourTree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grovegs%2FBehaviourTree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grovegs%2FBehaviourTree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grovegs%2FBehaviourTree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grovegs","download_url":"https://codeload.github.com/grovegs/BehaviourTree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252046033,"owners_count":21685935,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","behaviour-tree","dotnet","godot"],"created_at":"2024-11-12T17:05:26.838Z","updated_at":"2026-01-22T15:21:53.447Z","avatar_url":"https://github.com/grovegs.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# GroveGames Behaviour Tree\n\n[![Build Status](https://github.com/grovegs/BehaviourTree/actions/workflows/release.yml/badge.svg)](https://github.com/grovegs/BehaviourTree/actions/workflows/release.yml)\n[![Tests](https://github.com/grovegs/BehaviourTree/actions/workflows/tests.yml/badge.svg)](https://github.com/grovegs/BehaviourTree/actions/workflows/tests.yml)\n[![Latest Release](https://img.shields.io/github/v/release/grovegs/BehaviourTree)](https://github.com/grovegs/BehaviourTree/releases/latest)\n[![NuGet](https://img.shields.io/nuget/v/GroveGames.BehaviourTree)](https://www.nuget.org/packages/GroveGames.BehaviourTree)\n\nA 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.\n\n## Table of Contents\n- [Overview](#overview)\n- [Features](#features)\n- [Installation](#installation)\n- [Getting Started](#getting-started)\n  - [Creating a Behavior Tree](#creating-a-behavior-tree)\n  - [Example Diagram](#example-diagram)\n  - [Example Nodes](#example-nodes)\n  - [Example Tree](#example-tree)\n  - [Debugging](#debugging)\n- [Usage Example in Godot](#usage-example-in-godot)\n- [Customization](#customization)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Overview\n\nThis 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.\n\n## Features\n\n- **Modular Nodes**: Includes `Selector`, `Sequence`, `Decorator`, and custom action nodes.\n- **Blackboard System**: A shared data space for AI agents to store and retrieve contextual information.\n- **Godot Integration**: Works seamlessly within Godot using the node structure.\n- **Extensibility**: Easily add new node types and custom behaviors.\n\n## Installation\n\nInstall the package via .NET CLI:\n\n```bash\ndotnet add package GroveGames.BehaviourTree\n```\n\nFor Godot:\n\n```bash\ndotnet add package GroveGames.BehaviourTree.Godot\n```\n\n## Getting Started\n\n### Creating a Behavior Tree\n\nTo set up a behavior tree, create a class that inherits from `Tree` and override the `SetupTree` method to define the AI structure.\n\n### Example Diagram\n```mermaid\ngraph TD\n    Root(Root) --\u003e Selector\n    Selector --\u003e AttackSequence(Sequence: Attack Sequence)\n    Selector --\u003e DefendSequence(Sequence: Defend Sequence)\n    \n    AttackSequence --\u003e Condition1[Condition: IsEnemyVisible == true]\n    AttackSequence --\u003e Cooldown1[Cooldown]\n    Cooldown1 --\u003e Repeater1[Repeater]\n    Repeater1 --\u003e Attack[Attack]\n\n    DefendSequence --\u003e Condition2[Condition: IsUnderAttack == true]\n    DefendSequence --\u003e Cooldown2[Cooldown]\n    Cooldown2 --\u003e Repeater2[Repeater]\n    Repeater2 --\u003e Defend[Defend]\n```\n\n### Example Nodes\n\n#### Attack\n\n```csharp\npublic class Attack : Node\n{\n    public Attack(IParent parent) : base(parent) {}\n\n    public override NodeState Evaluate(float delta)\n    {\n        Console.WriteLine(\"Attacking\");\n        return NodeState.Running;\n    }\n}\n```\n\n#### Defend\n\n```csharp\npublic class Defend : Node\n{\n    public Defend(IParent parent) : base(parent) {}\n\n    public override NodeState Evaluate(float delta)\n    {\n        Console.WriteLine(\"Defending\");\n        return NodeState.Running;\n    }\n}\n```\n\n### Example Tree\nHere's an example of a `CharacterBT` class that builds an AI behavior tree:\n```csharp\npublic class CharacterBT : Tree\n{\n    public CharacterBT(GroveGames.BehaviourTree.Nodes.Root root) : base(root) { }\n\n    public override void SetupTree()\n    {\n        var selector = Root.Selector();\n        \n        var attackSequence = selector.Sequence();\n        attackSequence.Attach(new Condition(() =\u003e IsEnemyVisible()));\n        attackSequence\n        .Cooldown(1f)\n        .Repeater(RepeatMode.UntilSuccess)\n        .Attach(new Attack(attackSequence));\n\n        var defendSequence = selector.Sequence()\n        defendSequence.Attach(new Condition(() =\u003e IsUnderAttack()));\n        defendSequence\n        .Cooldown(1f)\n        .Repeater(RepeatMode.UntilSuccess)\n        .Attach(new Defend(defendSequence));\n    }\n}\n```\n\n### Debugging\n\nTo 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.\n\n1. **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:\n\n    ```csharp\n    public override void _Ready()\n    {\n        _characterBT = new CharacterBT();\n        _characterBT.SetRoot(new Root(new Blackboard()));\n        _characterBT.SetupTree();\n        _characterBT.Enable();\n        AddChild(_characterBT);\n    }\n    ```\n\n2. **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.\n\n3. **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.\n\n#### Example Visual Tree\nBelow is an example of how a behavior tree appears in the visual debugger, showing nodes and their states during runtime.\n\n![Example Visual Tree](docs/ExampleTree.png)\n\n## Usage Example in Godot\n\nBelow is a full example of setting up and using the behavior tree in a Godot scene:\n\n```csharp\npublic partial class Character : Godot.Node\n{\n    private CharacterBT _characterBT;\n\n    public override void _Ready()\n    {\n        _characterBT = new CharacterBT(new Root(new Blackboard()));\n        _characterBT.SetupTree();\n    }\n\n    public override void _Process(double delta)\n    {\n        _characterBT.Tick((float)delta);\n    }\n\n    public override void _Input(InputEvent @event)\n    {\n        if (@event.IsPressed())\n        {\n            _characterBT.Abort(); // Aborts the current tree\n        }\n    }\n}\n```\n\n## Customization\n\nExtend the framework with new functionality by creating custom nodes:\n\n- **Action Nodes**: Inherit from `Node` and implement specific behaviors in `Evaluate`.\n- **Decorator Nodes**: Inherit from `Decorator` and modify the behavior of a single child node.\n- **Composite Nodes**: Inherit from `Composite` and define logic for multiple child nodes.\n- **Blackboard**: Use the blackboard to share data between nodes. For example, store target information or flags.\n\n### Example: Custom Decorator (Delayer)\n\nThis `Delayer` decorator delays the execution of a child node by a specified amount of time:\n\n```csharp\npublic class Delayer : Decorator\n{\n    private readonly float _waitTime;\n    private float _interval;\n\n    public Delayer(IParent parent, float waitTime) : base(parent)\n    {\n        _waitTime = waitTime;\n    }\n\n    public override NodeState Evaluate(float deltaTime)\n    {\n        _interval += deltaTime;\n\n        if (_interval \u003e= _waitTime)\n        {\n            _interval = 0f;\n            return base.Evaluate(deltaTime);\n        }\n        else\n        {\n            return NodeState.Running;\n        }\n    }\n\n    public override void Reset()\n    {\n        _interval = 0f;\n    }\n}\n```\n\nThis decorator only allows the child node to execute once the specified wait time has passed.\n\n## Contributing\n\nContributions are welcome! To contribute:\n1. Fork the repository.\n2. Create a new branch (`git checkout -b feature/your-feature`).\n3. Commit your changes (`git commit -am 'Add new feature'`).\n4. Push the branch (`git push origin feature/your-feature`).\n5. Open a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrovegs%2Fbehaviourtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrovegs%2Fbehaviourtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrovegs%2Fbehaviourtree/lists"}