GithubHelp home page GithubHelp logo

aillieo / easybehaviortree Goto Github PK

View Code? Open in Web Editor NEW
30.0 5.0 5.0 1.28 MB

A very simple behavior tree implementation including a creator and runtime codes

License: MIT License

C# 100.00%
unity csharp behavior behavior-tree ai editor

easybehaviortree's Introduction

Easy Behavior Tree

A very simple behavior tree implementation, including:

  1. Basic behavior tree functions in C# but independent of any Unity Engine codes;

  2. A behavior tree can run totally without a GameObject;

  3. A behavior tree creator with Unity Editor in which binary format behavior tree files can be generated;

  4. Types of nodes, node parameters and blackboard data can be easily extended;

  5. Debugging system including logger interfaces and node info fomatters;

Requirement

The project is created with Unity 2018.3.6f, any other older or newer versions are not tested at the moment;

Usage

Create new nodes

  1. Create new node classes simply by extending the provided base types like NodeAction, NodeComposite, NodeCondition or NodeDecorator:

    using EasyBehaviorTree;
    [System.Serializable]
    public class NodeTest : NodeAction
    {
        // ...
    }
  2. Implement or override the methods:

    public class NodeTest : NodeAction
    {
        public override void Cleanup()
        {
            // your cleanup logic
        }
    
        protected override BTState ExecuteTask(float deltaTime)
        {
            // your execute logic
    
            // ...
    
            return BTState.Success;
        }
    }
  3. Add NodeParam attribute to a field of your class if you want it to be exposed and modified in Creator:

    public class NodeTest : NodeAction
    {
        [NodeParam]
        private int foo;
    
        [NodeParam]
        private float bar;
    
        // ...
    }
  4. If your node field has a type that is not included in NodeParamConfig, you can simply add the type name in NodeParamConfig.asset and click Regenerate:

    img

  5. Custom icons can be assigned to your node classes if you add a NodeIcon attribute to the class with the path of icon asset as argument:

    [System.Serializable]
    [NodeIcon("Assets/icon_for_node_test.png")]
    public class NodeTest : NodeAction
    {
        // ...
    }

Create behavior trees

  1. Create an empty prefab with the same name as your behavior tree;

  2. Create new empty gameobjects under the root of your prefab and a NodeDefine component should be attached to every gameobject in the hierarchy:

    img

  3. The structure of the prefab will be the the structure of your behavior tree, that means every gameobject with NodeDefine will be converted to a corresponding behavior tree node;

  4. Modify params of the nodes, change their names, or just add some description text in Unity inspector window;

  5. Save the prefab and right click it, and then select 'CreateTreeAsset' to create a behavior tree asset:

    img

  6. The following is a prefab named BT_Hero in sample directory:

    img

and the tree information with DefaultFormatter:

```
root(EasyBehaviorTree.NodeRepeater) {repeatTimes=100,ignoringFailure=False,oncePerTick=True}
-sel(EasyBehaviorTree.NodeSelector) {}
--seq(EasyBehaviorTree.NodeSequence) {}
---hasTar(HasTarget) {}
---sel(EasyBehaviorTree.NodeSelector) {}
----seq(EasyBehaviorTree.NodeSequence) {}
-----inRange(InAttackRange) {}
-----seq(EasyBehaviorTree.NodeSequence) {}
------atk(AttackTarget) {}
------wait(EasyBehaviorTree.NodeActionWait) {time=1}
----move(ApproachTarget) {}
--find(FindTarget) {}
```

Runtime operation

  1. Load a binary behavior tree asset with BehaviorTree.LoadBehaviorTree to create a behavior tree instance:

        // string fullPath = ...
        BehaviorTree behaviorTree = BehaviorTree.LoadBehaviorTree(fullPath);
  2. Restart should be called before you tick your behavior tree:

        behaviorTree.Restart();
  3. Tick should be called every frame (or driven by other systems) and the delta time should be passed as argument;

        void Update()
        {
            behaviorTree.Tick(Time.deltaTime);
        }
  4. You can listen the events like OnBehaviorTreeCompleted etc.;

        behaviorTree.OnBehaviorTreeCompleted +=
            (bt,st) => {
                Debug.Log("Tree completed with state = " + st);
            };
  5. Run scene 'Sample' for more details:

    img

    and hope you enjoy it ๐Ÿ˜„

easybehaviortree's People

Contributors

aillieo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.