Creating e new Behaviour Tree decorator (AtomsUnreal)
In this section, we are going to create a simple repeat behaviour tree decorator.
Create an UAtomsBehaviourTreeDecoratorNode
In your project, create a new class MyRepeatAtomsDecoratorNode and inherit from the UAtomsBehaviourTreeDecoratorNode .
UCLASS()
class UMyRepeatAtomsDecoratorNode : public UAtomsBehaviourTreeDecoratorNode
{
GENERATED_UCLASS_BODY()
};
You need to add these arguments to the UCLASS.
UCLASS(ClassGroup = (AtomsBehavioursTreeNode))
class UMyRepeatAtomsDecoratorNode : public UAtomsBehaviourTreeDecoratorNode
{
GENERATED_UCLASS_BODY()
};
Implement the constructor setting the AtomsBehaviourModule
member. This variable contains the name of the Atoms behaviour tree node that this unreal node creates.
UMyRepeatAtomsDecoratorNode::UMyRepeatAtomsDecoratorNode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
NodeName = "MyRepeat";
}
Implement the description string method.
Add properties
Now it is time to add some properties to the decorator node. If you are going to use blackboard entries on these properties you need to use the FAtomsBehaviourTreeNode*Property struct that you can find inside “BehaviourTree/AtomsBehaviourTreeNodeProperty.h” header.
For this decorator we need those 3 properties:
FAtomsBehaviourTreeNodeBoolProperty lockAgent
; //used to lock the agent mutex when accessing the blackboard
FAtomsBehaviourTreeNodeIntProperty repeatTimes
; // Number of repeats
FAtomsBehaviourTreeNodeBoolProperty repeatForever;
// Repeat forever
Property conversion
You need to implement now the GetAtomsAttribute method to pass the properties to the atoms node.
Create atoms node
The last step to do inside the unreal decorator node is to implement the GenerateAtomsBehaviour method that creates the equivalent atoms node MyRepeatAtomsBehaviourTreeDecoratorNode
we are going to implement after this step.
Implement the Atoms decorator class.
Our UMyRepeatAtomsDecoratorNode
class is just a description of a decorator node, there is no logic inside, it is used to be able to expose properties used by a native Atoms decorator node. Create a new MyRepeatAtomsBehaviourTreeDecoratorNode
class.
Add attributes to the Atoms decorator class
like we did with the UMyRepeatAtomsDecoratorNode
, we need to expose the same attributes here using the Atoms::Blackboard*Value classes. You don’t need to expose the lock attribute since it is already inside the base class.
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.
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.
Those are the final files:
Copyright © 2017, Toolchefs LTD.