Behavior Tree Custom Data Initialization

To implement your own behavior tree task, we simply subclass UBTTaskNode in code and write our own functionality. Sometimes we need our special instance memory (NodeMemory) if we are not instancing the node itself to store states. Looking at existing out of the box implementation, one can simply declare a struct like so:

struct MyTaskMemory
{
    FAIRequestID RequestID;
    uint8 bState0 : 1;
    float MyFloat;
}

A function called GetInstanceMemorySize() will return the size of this structure for the base class to allocate memory for this. My question is, how can we initialize the values inside that structure if we want something other than the default? For example, the FAIRequestID defaults to 0, which is not the default if we were to call the default constructor for FAIRequestID (-1 is the default through constructor). I suspect the base class simply use memzero() to zero out all memory.

Hey Oscar,

It’s easy to do! Just override UBTNode::InitializeMemory and set your custom memory struct’s properties to whatever you need :smiley:

Cheers,

–mieszko

Thank you! Does this get called only the first time when the node is being executed? Will it get called again after the task has succeed and being executed again?

NVM, found it!

Cool :slight_smile:

For the record, memory initialization is done only when a tree is being set up for a given BT component. This way memory can be used to store “persistent” data (memories! :D), independent from current BT flow.