Skip to content

Getting started

BehaviorTree.CPP is a C++ library that can be easily integrated into your favourite distributed middleware, such as ROS or SmartSoft.

You can statically link it into your application (for example a game).

These are the main concepts which you need to understand first.

Nodes vs Trees

The user must create his/her own ActionNodes and ConditionNodes (LeafNodes); this library helps you to compose them easily into trees.

Think about the LeafNodes as the building blocks which you need to compose a complex system.

By definition, your custom Nodes are (or should be) highly reusable. But, at the beginning, some wrapping interfaces might be needed to adapt your legacy code.

The tick() callbacks

Any TreeNode can be seen as a mechanism to invoke a callback, i.e. to run a piece of code. What this callback does is up to you.

In most of the following tutorials, our Actions will simply print messages on console or sleep for a certain amount of time to simulate a long calculation.

In production code, especially in Model Driven Development and Component Based Software Engineering, an Action/Condition would probably communicate to other components or services of the system.

Inheritance vs dependency injection.

To create a custom TreeNode, you should inherit from the proper class.

For instance, to create your own synchronous Action, you should inherit from the class SyncActionNode.

Alternatively, the library provides a mechanism to create a TreeNode passing a function pointer to a wrapper (dependency injection).

This approach reduces the amount of boilerplate in your code; as a reference please look at the first tutorial and the one describing non intrusive integration with legacy code.

Dataflow, Ports and Blackboard

Ports are explained in detail in the second and third tutorials.

For the time being, it is important to know that:

  • A Blackboard is a key/value storage shared by all the Nodes of a Tree.

  • Ports are a mechanism that Nodes can use to exchange information between each other.

  • Ports are "connected" using the same key of the blackboard.

  • The number, name and kind of ports of a Node must be known at compilation-time (C++); connections between ports are done at deployment-time (XML).

Load trees at run-time using the XML format

Despite the fact that the library is written in C++, trees themselves can be composed at run-time, more specifically, at deployment-time, since it is done only once at the beginning to instantiate the Tree.

An XML format is described in details here.