Git Product home page Git Product logo

fluid-behavior-tree's Introduction

Fluid Behavior Tree

Build Status

Behavior trees for Unity3D projects. Written with a code driven approach to maximize maintainability on large projects with the builder pattern. Inspired by Fluent Behavior Tree.

Features

  • Extendable, write your own custom re-usable nodes
  • Pre-built library of tasks to kickstart your AI
  • Tree visualizer to debug your trees at runtime
  • Heavily tested with TDD and unit tests
  • Tracks the last position of your behavior tree and restores it the next frame
  • Built for Unity (no integration overhead)

Support

Join the Discord Community if you have questions or need help.

See upcoming features and development progress on the Trello Board.

Getting Started

When creating trees you'll need to store them in a variable to properly cache all the necessary data.

using UnityEngine;
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Trees;

public class MyCustomAi : MonoBehaviour {
    [SerializeField]
    private BehaviorTree _tree;
    
    private void Awake () {
        _tree = new BehaviorTreeBuilder(gameObject)
            .Sequence()
                .Condition("Custom Condition", () => {
                    return true;
                })
                .Do("Custom Action", () => {
                    return TaskStatus.Success;
                })
            .End()
            .Build();
    }

    private void Update () {
        // Update our tree every frame
        _tree.Tick();
    }
}

What a Returned TaskStatus Does

Depending on what you return for a task status different things will happen.

  • Success: Node has finished, next tree.Tick() will restart the tree if no other nodes to run
  • Failure: Same as success, except informs that the node failed
  • Continue: Rerun this node the next time tree.Tick() is called. A pointer reference is tracked by the tree and can only be cleared if tree.Reset() is called.

Tree Visualizer

As long as your tree storage variable is set to public or has a SerializeField attribute. You'll be able to print a visualization of your tree while the game is running in the editor. Note that you cannot view trees while the game is not running. As the tree has to be built in order to be visualized.

Visualizer

Extending Trees

You can safely add new code to your behavior trees with several lines. Allowing you to customize BTs while supporting future version upgrades.

using UnityEngine;
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Tasks.Actions;
using CleverCrow.Fluid.BTs.Trees;

public class CustomAction : ActionBase {
    protected override TaskStatus OnUpdate () {
        Debug.Log(Owner.name);
        return TaskStatus.Success;
    }
}

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder CustomAction (this BehaviorTreeBuilder builder, string name = "My Action") {
        return builder.AddNode(new CustomAction { Name = name });
    }
}

public class ExampleUsage : MonoBehaviour {
    public void Awake () {
        var bt = new BehaviorTreeBuilder(gameObject)
            .Sequence()
                .CustomAction()
            .End();
    }
}

Installing

Fluid Behavior Tree is used through Unity's Package Manager. In order to use it you'll need to add the following lines to your Packages/manifest.json file. After that you'll be able to visually control what specific version of Fluid Behavior Tree you're using from the package manager window in Unity. This has to be done so your Unity editor can connect to NPM's package registry.

{
  "scopedRegistries": [
    {
      "name": "NPM",
      "url": "https://registry.npmjs.org",
      "scopes": [
        "com.fluid"
      ]
    }
  ],
  "dependencies": {
    "com.fluid.behavior-tree": "2.2.0"
  }
}

Archives of specific versions and release notes are available on the releases page.

Example Scene

You might want to look at the capture the flag example project for a working example of how Fluid Behavior Tree can be used in your project. It demonstrates real time usage with units who attempt to capture the flag while grabbing power ups to try and gain the upper hand.

Table of Contents

Example Scene

You might want to look at the capture the flag example project for a working example of how Fluid Behavior Tree can be used in your project. It demonstrates real time usage with units who attempt to capture the flag while grabbing power ups to try and gain the upper hand.

Library

Fluid Behavior Tree comes with a robust library of pre-made actions, conditions, composites, and other nodes to help speed up your development process.

Actions

Action Generic

You can create a generic action on the fly. If you find yourself re-using the same actions you might want to look into the section on writing your own custom actions.

.Sequence()
    .Do("Custom Action", () => {
        return TaskStatus.Success;
    })
.End()

Wait

Skip a number of ticks on the behavior tree.

.Sequence()
    // Wait for 1 tick on the tree before continuing
    .Wait(1)
    .Do(MyAction)
.End()

Wait Time

Waits until the passed number of seconds have expired in deltaTime.

.Sequence()
    .WaitTime(2.5f)
    .Do(MyAction)
.End()

Conditions

Condition Generic

You can create a generic condition on the fly. If you find yourself re-using the same actions you might want to look into the section on writing your own custom conditions.

.Sequence()
    .Condition("Custom Condtion", () => {
        return true;
    })
    .Do(MyAction)
.End()

RandomChance

Randomly evaluate a node as true or false based upon the passed chance.

.Sequence()
    // 50% chance this will return success
    .RandomChance(1, 2)
    .Do(MyAction)
.End()

Composites

Sequence

Runs each child node in order and expects a Success status to tick the next node. If Failure is returned, the sequence will stop executing child nodes and return Failure to the parent.

NOTE It's important that every composite is followed by a .End() statement. This makes sure that your nodes are properly nested when the tree is built.

.Sequence()
    .Do(() => { return TaskStatus.Success; })
    .Do(() => { return TaskStatus.Success; })
    
    // All tasks after this will not run and the sequence will exit
    .Do(() => { return TaskStatus.Failure; })

    .Do(() => { return TaskStatus.Success; })
.End()

Selector

Runs each child node until Success is returned.

.Selector()
    // Runs but fails
    .Do(() => { return TaskStatus.Failure; })

    // Will stop here since the node returns success
    .Do(() => { return TaskStatus.Success; })
    
    // Does not run
    .Do(() => { return TaskStatus.Success; })
.End()

SelectorRandom

Randomly selects a child node with a shuffle algorithm. Looks until Success is returned or every node fails. Shuffles every time the tree initially start running it.

.SelectorRandom()
    .Do(() => { return TaskStatus.Failure; })
    .Do(() => { return TaskStatus.Success; })
    .Do(() => { return TaskStatus.Failure; })
.End()

Parallel

Runs all child nodes at the same time until they all return Success. Exits and stops all running nodes if ANY of them return Failure.

.Parallel()
    // Both of these tasks will run every frame
    .Do(() => { return TaskStatus.Continue; })
    .Do(() => { return TaskStatus.Continue; })
.End()

Decorators

Decorators are parent elements that wrap any node to change the return value (or execute special logic). They are extremely powerful and a great compliment to actions, conditions, and composites.

Decorator Generic

You can wrap any node with your own custom decorator code. This allows you to customize re-usable functionality.

NOTE: You must manually call Update() on the child node or it will not fire. Also every decorator must be followed by a .End() statement. Otherwise the tree will not build correctly.

.Sequence()
    .Decorator("Return Success", child => {
        child.Update();
        return TaskStatus.Success;
    })
        .Do(() => { return TaskStatus.Failure; })
    .End()
    .Do(() => { return TaskStatus.Success; })
.End()

Inverter

Reverse the returned status of the child node if it's TaskStatus.Success or TaskStatus.Failure. Does not change TaskStatus.Continue.

.Sequence()
    .Inverter()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

ReturnSuccess

Return TaskStatus.Success if the child returns TaskStatus.Failure. Does not change TaskStatus.Continue.

.Sequence()
    .ReturnSuccess()
        .Do(() => { return TaskStatus.Failure; })
    .End()
.End()

ReturnFailure

Return TaskStatus.Failure if the child returns TaskStatus.Success. Does not change TaskStatus.Continue.

.Sequence()
    .ReturnFailure()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatForever

Return TaskStatus.Continue regardless of what status the child returns. This decorator (and all descendent tasks) can be interrupted by calling BehaviorTree.Reset().

.Sequence()
    .RepeatForever()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatUntilFailure

Return TaskStatus.Failure if the child returns TaskStatus.Failure, otherwise it returns TaskStatus.Continue.

.Sequence()
    .RepeatUntilFailure()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatUntilSuccess

Return TaskStatus.Success if the child returns TaskStatus.Success, otherwise it returns TaskStatus.Continue.

.Sequence()
    .RepeatUntilSuccess()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

Creating Reusable Behavior Trees

Trees can be combined with just a few line of code. This allows you to create injectable behavior trees that bundles different nodes for complex functionality such as searching or attacking.

Be warned that spliced trees require a newly built tree for injection, as nodes are only deep copied on .Build().

using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Tasks;
using UnityEngine;

public class MyCustomAi : MonoBehaviour {
    private BehaviorTree _tree;
    
    private void Awake () {
        var injectTree = new BehaviorTreeBuilder(gameObject)
            .Sequence()
                .Do("Custom Action", () => {
                    return TaskStatus.Success;
                })
            .End();

        _tree = new BehaviorTreeBuilder(gameObject)
            .Sequence()
                .Splice(injectTree.Build())
                .Do("Custom Action", () => {
                    return TaskStatus.Success;
                })
            .End()
            .Build();
    }

    private void Update () {
        // Update our tree every frame
        _tree.Tick();
    }
}

Creating Custom Reusable Nodes

What makes Fluid Behavior Tree so powerful is the ability to write your own nodes and add them to the builder without editing any source. You can even create Unity packages that add new builder functionality. For example we can write a new tree builder method like this that sets the target of your AI system with just a few lines of code.

var tree = new BehaviorTreeBuilder(gameObject)
    .Sequence()
        .AgentDestination("Find Enemy", target)
        .Do(() => {
            // Activate chase enemy code
            return TaskStatus.Success; 
        })
    .End()
    .Build();

Your First Custom Node and Extension

It should take about 3 minutes to create your first custom action and implement it. First create a new action.

using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Tasks.Actions;
using UnityEngine;
using UnityEngine.AI;

public class AgentDestination : ActionBase {
    private NavMeshAgent _agent;
    public Transform target;

    protected override void OnInit () {
        _agent = Owner.GetComponent<NavMeshAgent>();
    }

    protected override TaskStatus OnUpdate () {
        _agent.SetDestination(target.position);
        return TaskStatus.Success;
    }
}

Next we need to extend the BehaviorTreeBuilder script with our new AgentDestination action. For more information on C# class extensions see the official docs.

using CleverCrow.Fluid.BTs.Trees;

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder AgentDestination (this BehaviorTreeBuilder builder, string name, Transform target) {
        return builder.AddNode(new AgentDestination {
            Name = name,
            target = target,
        });
    }
}

And you're done! You've now created a custom action and extendable behavior tree builder that's future proofed for new versions. The following examples will be more of the same. But each covers a different node type.

Custom Actions

You can create your own custom actions with the following template. This is useful for bundling up code that you're using constantly.

using UnityEngine;
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Tasks.Actions;

public class CustomAction : ActionBase {
    // Triggers only the first time this node is run (great for caching data)
    protected override void OnInit () {
    }

    // Triggers every time this node starts running. Does not trigger if TaskStatus.Continue was last returned by this node
    protected override void OnStart () {
    }

    // Triggers every time `Tick()` is called on the tree and this node is run
    protected override TaskStatus OnUpdate () {
        // Points to the GameObject of whoever owns the behavior tree
        Debug.Log(Owner.name);
        return TaskStatus.Success;
    }

    // Triggers whenever this node exits after running
    protected override void OnExit () {
    }
}

Add your new node to an extension.

using CleverCrow.Fluid.BTs.Trees;

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder CustomAction (this BehaviorTreeBuilder builder, string name = "My Action") {
        return builder.AddNode(new CustomAction {
            Name = name,
        });
    }
}

Custom Conditions

Custom conditions can be added with the following example template. You'll want to use these for checks such as sight, if the AI can move to a location, and other tasks that require a complex check.

using UnityEngine;
using CleverCrow.Fluid.BTs.Tasks;

public class CustomCondition : ConditionBase {
    // Triggers only the first time this node is run (great for caching data)
    protected override void OnInit () {
    }

    // Triggers every time this node starts running. Does not trigger if TaskStatus.Continue was last returned by this node
    protected override void OnStart () {
    }

    // Triggers every time `Tick()` is called on the tree and this node is run
    protected override bool OnUpdate () {
        // Points to the GameObject of whoever owns the behavior tree
        Debug.Log(Owner.name);
        return true;
    }

    // Triggers whenever this node exits after running
    protected override void OnExit () {
    }
}

Add the new condition to your behavior tree builder with the following snippet.

using CleverCrow.Fluid.BTs.Trees;

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder CustomCondition (this BehaviorTreeBuilder builder, string name = "My Condition") {
        return builder.AddNode(new CustomCondition {
            Name = name,
        });
    }
}

Custom Composites

Fluid Behavior Tree isn't limited to just custom actions and conditions. You can create new composite types with a fairly simple API. Here is an example of a basic sequence.

using CleverCrow.Fluid.BTs.TaskParents.Composites;
using CleverCrow.Fluid.BTs.Tasks;

public class CustomSequence : CompositeBase {
    protected override TaskStatus OnUpdate () {            
        for (var i = ChildIndex; i < Children.Count; i++) {
            var child = Children[ChildIndex];

            var status = child.Update();
            if (status != TaskStatus.Success) {
                return status;
            }

            ChildIndex++;
        }

        return TaskStatus.Success;
    }
}

Adding custom composites to your behavior tree is just as simple as adding actions. Just takes one line of code.

using CleverCrow.Fluid.BTs.Trees;

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder CustomSequence (this BehaviorTreeBuilder builder, string name = "My Sequence") {
        return builder.ParentTask<CustomSequence>(name);
    }
}

Custom Decorators

Decorators can also be custom written to cut down on repetitive code.

using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

public class CustomInverter : DecoratorBase {
    protected override TaskStatus OnUpdate () {
        if (Child == null) {
            return TaskStatus.Success;
        }

        var childStatus = Child.Update();
        var status = childStatus;

        switch (childStatus) {
            case TaskStatus.Success:
                status = TaskStatus.Failure;
                break;
            case TaskStatus.Failure:
                status = TaskStatus.Success;
                break;
        }

        return status;
    }
}

Implementing decorators is similar to composites. If you need to set arguments on the composite you'll want to take a loot at the method BehaviorTreeBuilder.AddNodeWithPointer().

using CleverCrow.Fluid.BTs.Trees;

public static class BehaviorTreeBuilderExtensions {
    public static BehaviorTreeBuilder CustomInverter (this BehaviorTreeBuilder builder, string name = "My Inverter") {
        // See BehaviorTreeBuilder.AddNodeWithPointer() if you need to set custom composite data from arguments
        return builder.ParentTask<CustomInverter>(name);
    }
}

Formatting Issues

If you are using an auto formatter it will probably mangle your code formatting with the builder syntax. To avoid this you can turn off formatting like so in JetBrains Rider. If you need a specific IDE, it shouldn't be too hard to google the specific formatting disable comment you need.

// @formatter:off
_tree = new BehaviorTreeBuilder(gameObject)
    .Sequence()
        .Condition("Custom Condition", () => {
            return true;
        })
        .Do("Custom Action", () => {
            return TaskStatus.Success;
        })
    .End()
    .Build();
// @formatter:on

Nightly Builds

To access nightly builds of develop that are package manager friendly you'll need to manually edit your Packages/manifest.json as so.

{
    "dependencies": {
      "com.fluid.behavior-tree": "https://github.com/ashblue/fluid-behavior-tree.git#nightly"
    }
}

Note that to get a newer nightly build you must delete this line and any related lock data in the manifest, let Unity rebuild, then add it back. As Unity locks the commit hash for Git urls as packages.

Development Environment

If you wish to run to run the development environment you'll need to install node.js. Then run the following from the root once.

npm install

If you wish to create a build run npm run build from the root and it will populate the dist folder.

Making Commits

All commits should be made using Commitizen (which is automatically installed when running npm install). Commits are automatically compiled to version numbers on release so this is very important. PRs that don't have Commitizen based commits will be rejected.

To make a commit type the following into a terminal from the root

npm run commit

Pull Requests / Contributing

Please see the Contributing Guidelines document for more info.

Contributor Credits

Thanks goes to these wonderful people (emoji key):

Ash Blue
Ash Blue

πŸ’»
Jesse Talavera-Greenberg
Jesse Talavera-Greenberg

πŸ’»
PureSaltProductions
PureSaltProductions

πŸ““
Martin Duvergey
Martin Duvergey

πŸ›
call-stack
call-stack

πŸ›
Piotr Jastrzebski
Piotr Jastrzebski

πŸ’»
Sounghoo
Sounghoo

πŸ’»
TNThomas
TNThomas

πŸ› πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

fluid-behavior-tree's People

Contributors

allcontributors[bot] avatar ashblue avatar dependabot[bot] avatar hookssi avatar jessetg avatar semantic-release-bot avatar tnthomas 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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

fluid-behavior-tree's Issues

package.json conflicts when adding to OpenUPM

Hi, I wanted to add this package to the OpenUPM registry, but as mentioned here there are some issues with the fact that there are two package.json files in this project.

As discussed over there, this package has been published already, and adjusting the package.json to be compatible with OpenUPM might/will break the existing way of publishing it. In my opinion OpenUPM is the more "standard" way of distributing a Unity-specific package compared to the npmjs registry - any chance the suggested changes could be made?

Could also attempt to make the changes myself, but wanted to discuss with the maintainers here first before digging into it.

Cheers!

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing package.json file.

A package.json file at the root of your project is required to release on npm.

Please follow the npm guideline to create a valid package.json file.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

share code with server

our project have a online mode, we sperate our server code out of untiy view code.
but fluid-BT uses GameObject as tree owner, i have to change all the owner type to implement this case.
can we change the owner type as a interface?

Minor editor rendering glitch

Thanks for this very nice behavior tree implementation!

I found a minor cosmetic issue, which doesn't seem to affect the functionality or usability:

When rendering behavior trees in the editor, the horizontal lines are a bit off:
grafik

fluid-behavior-tree version: 2.2.2 (included using the Unity package manager)
Unity: 2019.4.10f1 Personal
OS: macOS 10.15.6

Decorated TaskBase does not invoke RemoveActiveTask.

First, Thank you for useful repo.
I have issue.
Decorated TaskBase does not invoke RemoveActiveTask in decorator.

  .Decorator(child=>{
              if(!context.CurrentIsDelay() || !context.CanActionTarget()){
                  child.ParentTree.RemoveActiveTask(child); // should do this for correct working
                  child.End();
                  return TaskStatus.Success;
              }
              
              return child.Update();
          })
              .AddNode(new CombatIdleAction(context.Animator))
          .End_Decorator()

Is this intentional?
How about that call RemoveActiveTask(child) in End() method ?

If i will get permit, i can make PR.
like this
TaskBase.cs

       private void Exit () {
            if (_exit) {
                OnExit();
            }
            if (_active) ParentTree?.RemoveActiveTask(this);
            Reset();
        }

Contribution Guidelines

A lot of people are making MRs without the proper commit guidelines. Create a PR template to with guidelines to decrease back and forth.

  • Specify to post new feature proposals and feedback to the discussions section
  • For basic troubleshooting (not bug reports) please ask questions on Discord
  • Indicate that commits must be done with commitlint standards (and how to do it)
  • Indicate test suite must pass manually
  • Point user to README.md section on developing

Test files should not be pulled into builds

Hi,

Installing NSubstitute with Unity package manager is required in order for Fluid Behavior Tree to compile properly.

Installation guidelines should mention that.

Best regards

Error every frame due to condition in spliced tree

.Sequence("Die")
    .Condition("Should Die?", () => State.HasFlag(CharacterState.Dead))
    .PathfindingStop()
    .AnimancerCrossFade(AnimationDictionary.Animations["Die"])
.End()
//.Sequence("Die Tree")
//    .Splice(UnitTasks.Die(gameObject, this, AnimationDictionary.Animations["Die"]))
//.End()

BT window shows all tasks as inactive when EditorApplication.isPaused

When I pause the editor, the behavior tree shows all the nodes as inactive.
This is unpractical because we want to ideally be able to pause and see exactly what our AI is doing at that particular frame.
image

(I am using Unity 2022.3.22f1)
I am surprised this issue hasn't been observed before, since when I look at the code it seems quite logical that there would be a bug there. I don't think it's Unity version related.

The state of activation of visual tasks is set to false inside of the OnGUI() method.
This works fine so long as the game is running. But the moment, the game is paused, the OnGUI() method is still running, which means all nodes become inactive.

The solution is to update the state of nodes in a method that runs at the same rate as the Update() function, in this case the EditorApplication.onUpdate. We can't do it in OnGUI because OnGUI() runs many times per frame and is completely unrelated to when the EditorApplication.isPaused is set properly.

=> This snippet is to show the first thing I tried and it doesn't work.
It's because EditorApplication.isPaused is not set to true before OnGUI has had the time to run several times already, so the nodes are set to false.

        public void Print ()
        {
            _printer.Print(_taskActive);

            if (!EditorApplication.isPaused)
                 _taskActive = false;
            

            foreach (var child in _children) {
                child.Print();
            }
        }

Anyway... here's the proper approach:
We print things in OnGUI. But we update values in EditorApplication.onUpdate... And we don't update values when the game is paused.

In BehaviorTreeWindow.cs

        void OnEnable() {
            EditorApplication.update += OnEditorUpdate;
        }

        void OnDisable() {
            EditorApplication.update -= OnEditorUpdate;
        }

        private void OnEditorUpdate() {
            if (!EditorApplication.isPaused)
                _printer?.UpdateValues();
        }

In BehaviorTreePrinter.cs

        public void UpdateValues()
        {
            _root.UpdateValues();
        }

In VisualTask.cs

      public void Print ()
        {
            _printer.Print(_taskActive);

            // _taskActive = false; // <= comment this out

            foreach (var child in _children) {
                child.Print();
            }
        }

        public void UpdateValues()
        {
            _printer.UpdateValues(_taskActive);
            _taskActive = false;
        
            foreach (var child in _children) {
                child.UpdateValues();
            }
        }

In NodePrinterController.cs

      public void UpdateValues(bool _taskIsActive)
        {
            _faders.Update(_taskIsActive);
        }

Now our GUI logic is separated from our value updating logic, and the nodes are keeping their active state even when the game is paused!
image

PATH_PROJECT in AssetPath describes a folder that never exists

Current project structure of the repo contains Assets/com.fluid.behavior-tree.
The PATH_PROJECT constant in AssetPath.cs is set to Assets/FluidBehaviorTree.
Due to this mismatch, the following console output occurs when attempting to view a behavior tree in the project:
image

Inject tree in existing tree during runtime

I've seen there is the option to inject a tree in the build process (example in the readme).
However, the Splice method also seems to allow injecting a tree during runtime into an existing one.

public void Splice(ITaskParent parent, BehaviorTree tree);

However, how can I get the correct parent node from my existing tree? Is this feasible?
Would be nice to have something like "BehaviorTree.GetNodeById(string id)" or alike.

Example use case: I plan to have a behavior tree for units like

  • selector
  • if attacked -> fight
  • if health is low -> flee
  • else do your work ->* injected job specific tree here*

My units will be assigned to different jobs during their lives (or no job at all) so at the "work" node I'd like to inject/replace a tree "woodcutterbehavior" or "farmerbehavior" or alike. At runtime, obviously.

Sorry if this is kind of a noob question, I'm only just getting familiar with Behavior Trees.

Example missing?

Hello,
In the examples, it looks like there is only a blank scene? Am I missing something by chance?

Thanks,
-MH

Repeat Tasks - Not functioning as expected

Repeat tasks are not working as expected. This issue only affects repeaters. Current bugs are as follows.

  • Does not run Start() and Exit() when a task ends, causing tasks like Wait to automatically fail on 2nd run
  • Composites do not work with any repeat tasks

This should by fixable by doing the following. Adequate test coverage and testing will be needed to verify this doesn't create any weird bugs in the app.

  • Prove the issue with a test that shows a sequence/composite does not repeat on completion
  • Write a test to verify nested composites on repeat run as expected repeat
  • Prove the issue with a test that shows Start() and Exit() fails to run task repeat
  • Write a test to verify nested tasks inside a composite run as expected on repeat
  • TaskParentBase should run a self Reset() when success or failure returns
  • TaskBase then needs to run a Reset() on Exit

Testing when the fix is ready:

  • Tree visualizer still works as expected
  • Tree visualizer doesn't blow out with nested repeats
  • Add a nested repeat sample to the sample project that verifies the new repeater logic works as expected

Logic mislead in TaskParentBase

In TaskParentBase we have an End method, but this method does not execute. You can check this in TaskParentTest.

So that produces an interesting case when you need to understand was a Parallel task completed. Because all tasks invoke End themselves.

Parallel:
    - ActionA (Success)
    - ActionB (Continue)

Parallel say us that End will be called when all tasks are completed. But in that case, ActionA call End at the same time us returns Success status.

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.