Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

50 Commits

Repository files navigation

BrainTree

A C++ behavior tree single header library.

Features

  • behavior tree implementation
  • predefined composites
  • predefined decorators
  • (optional) rudimentary blackboard
  • (optional) behavior tree builders

Install

Include BrainTree.h in your project.

Example

// this example should print out "Hello, World!", four times
#include<iostream>
#include"BrainTree.h"classAction : publicBrainTree::Node
{
public:
Status update() override
{
std::cout << "Hello, World!" << std::endl;
return Node::Status::Success;
}
};
voidCreatingBehaviorTreeManually()
{
BrainTree::BehaviorTree tree;
auto sequence = std::make_shared<BrainTree::Sequence>();
auto sayHello = std::make_shared<Action>();
auto sayHelloAgain = std::make_shared<Action>();
sequence->addChild(sayHello);
sequence->addChild(sayHelloAgain);
tree.setRoot(sequence);
tree.update();
}
voidCreatingBehaviorTreeUsingBuilders()
{
auto tree = BrainTree::Builder()
.composite<BrainTree::Sequence>()
.leaf<Action>()
.leaf<Action>()
.end()
.build();
tree->update();
}
intmain()
{
CreatingBehaviorTreeManually();
CreatingBehaviorTreeUsingBuilders();
return0;
}

Composites

CompositeBehaviourSuccessRunningFailure
SelectorTicks each child node in order, starting from the beginning each tickIf a child succeedsIf a child is runningIf all children fail
SequenceTicks each child node in order, starting from the beginning each tickIf all children succeedIf a child is runningIf a child fails
StatefulSelectorTicks each child node in order, starting from the child ticked in previous tickIf a child succeedsIf a child is runningIf all children fail
StatefulSequenceTicks each child node in order, starting from the child ticked in previous tickIf all children succeedIf a child is runningIf a child fails

Decorators

DecoratorBehaviour
SucceederAlways returns success
FailerAlways returns failure
InverterInverts the result of the child node
RepeaterRepeats until child node succeeds (infinitely or limited)
UntilSuccessRepeats until child node succeeds
UntilFailureRepeats until child node fails

Builder

The Builder class simplifies the process of creating a behavior tree. You use three methods to build your tree:

  • leaf<NodeType>()
  • composite<CompositeType>()
  • decorator<DecoratorType>()

Both composite() and decorator() require a corresponding call to end(), this marks where you are done adding children to a composite or a child to a decorator. At the very end you call build() which will then give you the finished behavior tree.

auto tree = Builder()
.decorator<Repeater>()
.composite<Sequence>()
.leaf<SayHello>("Foo")
.leaf<SayHello>("Bar")
.end()
.end()
.build();

License

MIT (c) Pär Arvidsson 2015-2018

Releases

Packages

Used by

Contributors

Languages