Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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

Create an UAtomsBehaviourTreeTaskNode

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.

UCLASS(ClassGroup = (AtomsBehavioursTreeNode))
class UMyRepeatAtomsDecoratorNode: public UAtomsBehaviourTreeTaskNode
{
  GENERATED_UCLASS_BODY()
  
  public:
  
    virtual FString GetStaticDescription() const override;
}
FString UMyRepeatAtomsDecoratorNode::GetStaticDescription() const
{
	return FString("My Custom Repat Decorator");
}

Add properties

Now it is time to add some properties to the task 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 wait task we need those 2 properties:

FAtomsBehaviourTreeNodeBoolProperty lockAgent; //used to lock the agent mutex when accessing the blackboard

FAtomsBehaviourTreeNodeIntProperty repeatTimes; // Number of repeats

FAtomsBehaviourTreeNodeBoolProperty repeatForever; // Repeat forever

#pragma once

#include "CoreMinimal.h"
#include "BehaviourTree/AtomsBehaviourTreeTaskNode.h"
#include "BehaviourTree/AtomsBehaviourTreeNodeProperty.h"
#include "MyRepeatAtomsDecoratorNode.generated.h"

UCLASS(ClassGroup = (AtomsBehavioursTreeNode))
class UMyRepeatAtomsDecoratorNode: public UAtomsBehaviourTreeTaskNode
{
	GENERATED_UCLASS_BODY()
	
public:

	virtual FString GetStaticDescription() const override;

public:

	/** If on, ensures no data on the agent is written simultaneously by nodes that have this option active. */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty lockAgent;

  /** Repeat the child forever */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty repeatForever;

/*  * Number of times to execute the child */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeIntProperty repeatTimes;
};

Property conversion

You need to implement now the GetAtomsAttribute method to pass the properties to the atoms node.

#pragma once

#include "CoreMinimal.h"
#include "BehaviourTree/AtomsBehaviourTreeTaskNode.h"
#include "BehaviourTree/AtomsBehaviourTreeNodeProperty.h"
#include "MyRepeatAtomsDecoratorNode.generated.h"

UCLASS(ClassGroup = (AtomsBehavioursTreeNode))
class UMyRepeatAtomsDecoratorNode: public UAtomsBehaviourTreeTaskNode
{
	GENERATED_UCLASS_BODY()
	
public:

	virtual FString GetStaticDescription() const override;

  virtual void GetAtomsAttribute(AtomsCore::MapMetadata* attributes) const override;

public:

	/** If on, ensures no data on the agent is written simultaneously by nodes that have this option active. */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty lockAgent;

  /** Repeat the child forever */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty repeatForever;

/*  * Number of times to execute the child */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeIntProperty repeatTimes;
};

UMyRepeatAtomsDecoratorNode::UMyRepeatAtomsDecoratorNode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	NodeName = "MyRepeat";
}

FString UMyRepeatAtomsDecoratorNode::GetStaticDescription() const
{
	return FString("My Custom Repeat Decorator");
}

void UMyRepeatAtomsDecoratorNode::GetAtomsAttribute(AtomsCore::MapMetadata* attributes) const
{
	lockAgent.GetAtomsAttribute(FString("lockAgent"), attributes);
	repeatForever.GetAtomsAttribute(FString("repeatForever"), attributes);
	repeatTimes.GetAtomsAttribute(FString("repeatTimes"), attributes);
}

Create atoms node

The last step to do inside the unreal task node is to implement the GenerateAtomsBehaviour method that creates the equivalent atoms node MyRepeatAtomsBehaviourTreeDecoratorNodewe are going to implement after this step.

#pragma once

#include "CoreMinimal.h"
#include "BehaviourTree/AtomsBehaviourTreeTaskNode.h"
#include "BehaviourTree/AtomsBehaviourTreeNodeProperty.h"
#include "MyRepeatAtomsDecoratorNode.generated.h"

UCLASS(ClassGroup = (AtomsBehavioursTreeNode))
class UMyRepeatAtomsDecoratorNode: public UAtomsBehaviourTreeTaskNode
{
	GENERATED_UCLASS_BODY()
	
public:

	virtual FString GetStaticDescription() const override;
	
	virtual void GetAtomsAttribute(AtomsCore::MapMetadata* attributes) const override;
	
	virtual Atoms::Behaviour* GenerateAtomsBehaviour(AActor* Actor, UActorComponent* Component) override;

public:

	/** If on, ensures no data on the agent is written simultaneously by nodes that have this option active. */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty lockAgent;

  /** Repeat the child forever */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeBoolProperty repeatForever;

    /** Number of times to execute the child */
	UPROPERTY(EditAnywhere, Category="Properties")
	FAtomsBehaviourTreeNodeIntProperty repeatTimes;
};
UMyRepeatAtomsDecoratorNode::UMyRepeatAtomsDecoratorNode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	NodeName = "MyRepeat";
}

FString UMyRepeatAtomsDecoratorNode::GetStaticDescription() const
{
	return FString("My Custom Repeat Decorator");
}

void UMyRepeatAtomsDecoratorNode::GetAtomsAttribute(AtomsCore::MapMetadata* attributes) const
{
	lockAgent.GetAtomsAttribute(FString("lockAgent"), attributes);
	repeatForever.GetAtomsAttribute(FString("repeatForever"), attributes);
	repeatTimes.GetAtomsAttribute(FString("repeatTimes"), attributes);
}

Atoms::Behaviour* UMyRepeatAtomsDecoratorNode::GenerateAtomsBehaviour(AActor* Actor, UActorComponent* Component)
{
	MyRepeatAtomsBehaviourTreeDecoratorNode* behaviour = new MyRepeatAtomsBehaviourTreeDecoratorNode();
	return behaviour;
}

Implement the Atoms decorator class.

Our UMyRepeatAtomsDecoratorNodeclass is just a description of a task 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 MyRepeatAtomsBehaviourTreeDecoratorNodeclass.

#include <Atoms/BehaviourTree/Decorator.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(); }
	
public:

};

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.

#include <Atoms/BehaviourTree/Decorator.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(); }
	
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.

#include <Atoms/BehaviourTree/Decorator.h>
#include <AtomsCore/Metadata/IntMetadata.h>
#include <AtomsCore/Metadata/DoubleMetadata.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(); }
	
	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);
	}
	
public:
	
    Atoms::BlackboardIntValue repeatTimes;
    
    Atoms::BlackboardBoolValue repeatForever;
};

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.

  • 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.

#include <Atoms/BehaviourTree/Decorator.h>
#include <AtomsCore/Metadata/IntMetadata.h>
#include <AtomsCore/Metadata/DoubleMetadata.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(); }
	
	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 initialize(BehaviourTreeContext* context, State* data) override;

	virtual void update(BehaviourTreeContext* context, State* data) override;

	virtual void onChildUpdated(BehaviourTreeContext* context, State* data, unsigned int childIndex, unsigned short childStatus) override;
	
public:
	
    Atoms::BlackboardIntValue repeatTimes;
    
    Atoms::BlackboardBoolValue repeatForever;
};

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.

		struct State
		{
			union
			{
				int intValue;
				int int2Value[2];
				short short4Value[4];
				float float2Value[2];
				double doubleValue;
				void* ptrValue;
			};
			unsigned short status;
			unsigned short index;
			unsigned short tickCount;
			unsigned short padding;
		};

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

struct MyStruct
{
  std::string foo1;
  double foo2
  AtomsMath::Vector3 foo3;
}

data->ptrValue = context->allocateMemory<MyStruct>();

To release the allocated data call

context->releaseMemory(reinterpret_cast<MyStruct>(data->ptrValue));

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.

void MyRepeatAtomsBehaviourTreeDecoratorNode::initialize(Atoms::BehaviourTreeContext* context, State* data)
{

    // It is a decorator so put it in SUSPENDED mode when it is initialized
	data->status = Status::SUSPENDEND;
	// Local storage, int2Value[0] will contains the number of repeats, 
	// data->int2Value[1] contains 1 it is waiting for the child
	data->int2Value[0] = 0;
	data->int2Value[1] = 0;
}

void MyRepeatAtomsBehaviourTreeDecoratorNode::update(Atoms::BehaviourTreeContext* context, State* data)
{
    // Check if it is already waiting for a child status
	if (data->int2Value[1] == 0)
	{
		// check if it need to repeat again the child
		if (context->getBlackboardValue(repeatForever) || (data->int2Value[0] <= context->getBlackboardValue(repeatTimes)))
		{
			// Flag as wait for child
			data->int2Value[1] = 1;
			// Start che child
			if (!context->start(childIndex))
				data->status = Status::FAILURE;
		}
	}
}

void MyRepeatAtomsBehaviourTreeDecoratorNode::onChildUpdated(Atoms::BehaviourTreeContext* context, State* data, unsigned int childIdx, unsigned short childStatus)
{

    // If the child is running than wait for a different status
	if (childStatus == Status::RUNNING)
	{
		data->status = Status::RUNNING;
		return;
	}

    // A child returned success or failure is it has terminated
	if (childStatus != Status::RUNNING)
		data->int2Value[1] = 0;

	if (childStatus == Status::SUCCESS)
	{
		if (lock)
		{
			context->agent->lock();
		}
		if (!context->getBlackboardValue(repeatForever) && ++data->int2Value[0] == context->getBlackboardValue(repeatTimes))
		{
			data->status = childStatus;
		}
		else
		{
			data->status = Status::RUNNING;
		}
		if (lock)
		{
			context->agent->unlock();
		}
		return;
	}

	if (childStatus == Status::FAILURE)
	{
		data->status = childStatus;
		return;
	}
}

Those are the final files:

  • No labels