Skip to content

EugenyN/BehaviorTrees

Repository files navigation

Behavior Trees

A simple C# example of Behavior Trees + Editor.

Introduction

Behavior Trees (BT) is a simple, scalable, modular solution that represents complex AI-behaviors and provides easy-to-maintain and configure logic.

They have been widely used in robotics and gaming since mid 2000, in particular, such game engines as Unreal, Unity, CryEngine use BT. You can learn more about Behavior Trees at the following links:

About project

This project demonstrates the concept and working principle of the Behavior Trees. Therefore, I tried to make it as simple and laconic as possible. You can fork, adapt and extend the project to suit your needs.

  • The project includes BehaviorTrees library with the main types of nodes: actions, conditions, composites and decorators (20 in total) as well as auxiliary classes. You can add your nodes by inheriting from existing ones. You can use ActionBase base class to create custom actions and use BaseEvent base class to create custom events. Trees can be serialized in json.

  • BehaviorTreesEditor allows you to edit trees with a simple TreeList control, to save, to load and to run trees.

Behavior Trees

Example 1

  • BehaviorTrees.Example1 contains simple example of the Behavior Tree with custom node Move:
[DataContract]
[BTNode("Move", "Example")]
public class Move : Node
{
    Point _position;
    bool _completed;

    [DataMember]
    public Point Position
    {
        get { return _position; }
        set {
            _position = value;
            Root.SendValueChanged(this);
        }
    }

    public Move(Point pos)
    {
        Position = pos;
    }

    public override string NodeParameters => $"Move To ({Position.X}, {Position.Y})";

    protected override void OnActivated()
    {
        base.OnActivated();
        Log.Write($"{Owner.Name} moving to {Position}");
        Task.Delay(1000).ContinueWith(_ => _completed = true);
    }

    protected override void OnDeactivated()
    {
        base.OnDeactivated();
        Log.Write($"{Owner.Name} moving completed ");
    }

    protected override ExecutingStatus OnExecuted()
    {
        return _completed ? ExecutingStatus.Success : ExecutingStatus.Running;
    }
}

BehaviorTrees.Example1 demonstrates Behavior Tree creation by using TreeBuilder:

var exampleBT = new TreeBuilder<Node>(new Sequence())
    .AddWithChild(new Loop(3))
        .AddWithChild(new Sequence())
            .Add(new Move(new Point(0, 0)))
            .Add(new Move(new Point(20, 0)))
            .AddWithChild(new Delay(2))
                .Add(new Move(new Point(0, 20)))
            .Up()
        .Up()
    .Up()
.ToTree();

Example 2 - IronPython nodes

The library BehaviorTrees.IronPython is an example of scripting language integration into the Behavior Tree. As an example two nodes are added: ScriptedAction and ScriptedCondition. The script can be edited both with help of the PropertyGrid and using the syntax highlighting editor implemented in BehaviorTrees.IronPython.Editor project.

Behavior Trees

You can extend the example and add your own API, import your types, both from the host application and from other IronPython scripts. Based on this example you can integrate another scripting language you need.

Build

  • net6.0 TFM is used for libraries and net6.0-windows for editors.
  • Please rebuild the solution to execute post-build scripts that will copy the example libraries to the output directory.

Third party in this project

Other Behavior Trees on Github

License

Copyright (c) 2015 Eugeny Novikov. Code under MIT license.

About

A simple C# example of Behavior Trees + Editor.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages