Creating a new Behaviour Tree decorator

In this section, we are going to create a simple repeat behaviour tree decorator.

Implement the Atoms decorator class.

Create a new MyRepeatAtomsBehaviourTreeDecoratorNodeclass.

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

Add attributes to the Atoms decorator class

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

#include <Atoms/BehaviourTree/Decorator.h> #include <Atoms/BehaviourTree/BlackboardValue.h> #include <Atoms/BehaviourTree/ConditionValue.h> class MyRepeatAtomsBehaviourTreeDecoratorNode: public Atoms::Decorator { public: MyRepeatAtomsBehaviourTreeDecoratorNode(): Atoms::Decorator(), repeatTimes(1), repeatForever(false) {} virtual ~MyRepeatAtomsBehaviourTreeDecoratorNode() {} static const char* staticTypeName() { return "MyRepeat"; } virtual const char* typeName() const override { return staticTypeName(); } static Atoms::Behaviour* creator() { return new MyRepeatAtomsBehaviourTreeDecoratorNode(); } public: Atoms::BlackboardIntValue repeatTimes; Atoms::BlackboardBoolValue repeatForever; };

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.

Then implement the getAttributes and getAttributesProperty. These methods are used by the behaviour tree editor to inspect and customize the widget for this node.

#include <Atoms/BehaviourTree/Decorator.h> #include <AtomsCore/Metadata/IntMetadata.h> #include <AtomsCore/Metadata/DoubleMetadata.h> #include <AtomsCore/Metadata/StringMetadata.h> #include <Atoms/BehaviourTree/BehaviourTreeContext.h> class MyRepeatAtomsBehaviourTreeDecoratorNode: public Atoms::Decorator { public: MyRepeatAtomsBehaviourTreeDecoratorNode(): Atoms::Decorator(), repeatTimes(1), repeatForever(false) {} virtual ~MyRepeatAtomsBehaviourTreeDecoratorNode() {} static const char* staticTypeName() { return "MyRepeat"; } virtual const char* typeName() const override { return staticTypeName(); } static Atoms::Behaviour* creator() { return new MyRepeatAtomsBehaviourTreeDecoratorNode(); } virtual void setAttributes(const AtomsCore::MapMetadata* attributes, Atoms::Blackboard* blackboard) override { if (!attributes) return; Atoms::Decorator::setAttributes(attributes, blackboard); Atoms::getBlackboardValueFromAttributes<AtomsCore::IntMetadata, int>(attributes, blackboard, "repeatTimes", repeatTimes); Atoms::getBlackboardValueFromAttributes<AtomsCore::BoolMetadata, bool>(attributes, blackboard, "repeatForever", repeatForever); } virtual void getAttributes(AtomsCore::MapMetadata* attributes, Blackboard* blackboard) override { if (!attributes) return; Decorator::getAttributes(attributes, blackboard); BT_BEHAVIOUR_DEFINE_GET_ATTRIBUTE(AtomsCore::IntMetadata, repeatTimes); BT_BEHAVIOUR_DEFINE_GET_ATTRIBUTE(AtomsCore::BoolMetadata, repeatForever); } virtual void getAttributeProperties(AtomsCore::MapMetadata* attributes) override { if (!attributes) return; Decorator::getAttributeProperties(attributes); auto parameters = attributes->getTypedEntry<AtomsCore::MapMetadata>(Atoms::GlobalNameKeys::ATOMS_BEHAVIOUR_TREE_NODE_PARAMETERS_KEY); AtomsCore::StringMetadata description("Repeats the child node either a number of times or forever.<br/><br/><b>Success</b>: if the child node return Success on the last repeat.<br/><br/><b>Failure</b>: if the child node return Failure on the last repeat.<br/><br/><b>Running</b>: As long as it is repeating."); attributes->addEntry(GlobalNameKeys::ATOMS_BEHAVIOUR_TREE_NODE_DESCRIPTION_KEY, &description); AtomsCore::StringMetadata tooltip; AtomsCore::MapMetadata attributeP; tooltip.set("Number of times to execute the child"); attributeP.addEntry(Atoms::GlobalNameKeys::ATOMS_BEHAVIOUR_TREE_PARAMETER_DESCRIPTION_KEY, &tooltip); parameters->addEntry("repeatTimes", &attributeP); tooltip.set("Repeat the child forever"); attributeP.addEntry(Atoms::GlobalNameKeys::ATOMS_BEHAVIOUR_TREE_PARAMETER_DESCRIPTION_KEY, &tooltip); parameters->addEntry("repeatForever", &attributeP); } public: Atoms::BlackboardIntValue repeatTimes; Atoms::BlackboardBoolValue repeatForever; };

It is time to implement the node logic. For a decorator 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.

  • onChildUpdated: called when a child changes its status.

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.

For decorators and composites node always set at the initialization a SUSPENDED state. Because it needs to wait for for a child update to know if it need to switch to RUNNING/SUCCESS or FAILURE state.

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

Windows:

In visual studio create a dll projects, add the atoms includes and add BUILD_ATOMSPLUGIN to the preprocessor definitions.

Linux:

Compile as a shared object adding the atoms includes and BUILD_ATOMSPLUGIN to the preprocessor definitions.

 

Copyright © 2017, Toolchefs LTD.