Creating a new Behaviour Tree task

In this section, we are going to create a simple wait behaviour tree task.

Implement the Atoms behaviour class.

Create a new MyWaitAtomsBehaviourTreeTaskNode class.

#include <Atoms/BehaviourTree/Behaviour.h> #include <Atoms/BehaviourTree/BlackboardValue.h> class MyWaitAtomsBehaviourTreeTaskNode : public Atoms::Behaviour { public: MyWaitAtomsBehaviourTreeTaskNode (): Atoms::Behaviour() {} virtual ~MyWaitAtomsBehaviourTreeTaskNode () {} static const char* staticTypeName() { return "MyWait"; } virtual const char* typeName() const override { return staticTypeName(); } static Atoms::Behaviour* creator(); public: }; Atoms::Behaviour* MyWaitAtomsBehaviourTreeTaskNode ::creator() { return new MyWaitAtomsBehaviourTreeTaskNode (); }

Add attributes to the Atoms behaviour class

We need to expose the same attributes here using the Atoms::Blackboard*Value classes.

#include <Atoms/BehaviourTree/Behaviour.h> #include <Atoms/BehaviourTree/BlackboardValue.h> class MyWaitAtomsBehaviourTreeTaskNode : public Atoms::Behaviour { public: MyWaitAtomsBehaviourTreeTaskNode (): Atoms::Behaviour(), waitTime(1.0), useSeconds(false) {} virtual ~MyWaitAtomsBehaviourTreeTaskNode () {} static const char* staticTypeName() { return "MyWait"; } virtual const char* typeName() const override { return staticTypeName(); } static Atoms::Behaviour* creator(); public: Atoms::BlackboardDoubleValue waitTime; bool useSeconds; }; Atoms::Behaviour* MyWaitAtomsBehaviourTreeTaskNode ::creator() { return new MyWaitAtomsBehaviourTreeTaskNode (); }

We can now initialize the attributes from the tree and blackboard definition. You need to implement the setAttributes method. It’s here that the blackboard entries are connected to each attribute.

#include <AtomsCore/Metadata/BoolMetadata.h> #include <AtomsCore/Metadata/DoubleMetadata.h> #include <Atoms/BehaviourTree/BehaviourTreeContext.h> class MyWaitAtomsBehaviourTreeTaskNode : public Atoms::Behaviour { public: MyWaitAtomsBehaviourTreeTaskNode (): Atoms::Behaviour(), waitTime(1.0), useSeconds(false) {} virtual ~MyWaitAtomsBehaviourTreeTaskNode () {} static const char* staticTypeName() { return "MyWait"; } virtual const char* typeName() const override { return staticTypeName(); } virtual void setAttributes(const AtomsCore::MapMetadata* attributes, Atoms::Blackboard* blackboard) override { if (!attributes) return; Atoms::Behaviour::setAttributes(attributes, blackboard); Atoms::getBlackboardValueFromAttributes<AtomsCore::DoubleMetadata, double>(attributes, blackboard, "waitTime", waitTime); auto useSecondsPtr = attributes->getTypedEntry<AtomsCore::BoolMetadata>("useSeconds"); if (useSecondsPtr) useSeconds = useSecondsPtr->value(); } static Atoms::Behaviour* creator(); public: Atoms::BlackboardDoubleValue waitTime; bool useSeconds; }; Atoms::Behaviour* MyWaitAtomsBehaviourTreeTaskNode ::creator() { return new MyWaitAtomsBehaviourTreeTaskNode (); }

Then we need to implement the getAttributes and getAttributesProperties. Those methods are used by the behaviour tree editor. The editor calls the getAttributes to know which attributes the node has. The getAttributesProperties is used to customize the appearance of the attributes inside the editor.

It is time to implement the node logic. For a task node, you can implement these methods:

  • initialize: method called to initialize the node instance.

  • update: method called at every tick.

  • terminate: method called when the node returned success/failure.

  • releaseData: called before the destruction of this node instance. Clear here all data allocated on the heap.

For this wait node, we need to implement the initialize to store the init time and then the update to check the timer is elapsed.

You must return a valid status that could be SUCCESS/FAILURE or RUNNING.

Local storage

Each node instance has local storage of 8 bytes that you can use to store data. The storage is defined inside a union inside the State structure.

 

If you need to allocate more then 8 byte do something like this:

To release the allocated data call

Please remember to release the allocated data at least inside the releaseData method.

 

Access attribute values

To access an attribute value you need to use the context->getBlackboardValue() method. This method automatically returns a reference to the attribute value or to the blackboard entry connected to this attribute.

Now it is time to register the node in a plugin

 

Compile

Use this cmake template:

 

Copyright © 2017, Toolchefs LTD.