Add releasing nested nodes state when BT tree goes irrelevant

This commit is contained in:
Wojtek Figat
2023-08-23 13:43:46 +02:00
parent 3259af3368
commit 73c0758410
3 changed files with 32 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ API_CLASS(Abstract) class FLAXENGINE_API BehaviorTreeNode : public SerializableS
friend class BehaviorTreeGraph;
friend class BehaviorKnowledge;
friend class BehaviorTreeSubTreeNode;
friend class BehaviorTreeCompoundNode;
protected:
// Raw memory byte offset from the start of the behavior memory block.
@@ -86,4 +87,8 @@ public:
ASSERT((int32)sizeof(T) <= GetStateSize());
return reinterpret_cast<T*>((byte*)memory + _memoryOffset);
}
protected:
virtual void InvokeReleaseState(const BehaviorUpdateContext& context);
};
};

View File

@@ -56,8 +56,7 @@ BehaviorUpdateResult BehaviorTreeNode::InvokeUpdate(const BehaviorUpdateContext&
// Check if node is not relevant anymore
if (result != BehaviorUpdateResult::Running)
{
relevantNodes.Set(_executionIndex, false);
ReleaseState(context.Behavior, context.Memory);
InvokeReleaseState(context);
}
return result;
@@ -79,6 +78,13 @@ void BehaviorTreeNode::Deserialize(DeserializeStream& stream, ISerializeModifier
DESERIALIZE(Name);
}
void BehaviorTreeNode::InvokeReleaseState(const BehaviorUpdateContext& context)
{
BitArray<>& relevantNodes = *(BitArray<>*)context.RelevantNodes;
relevantNodes.Set(_executionIndex, false);
ReleaseState(context.Behavior, context.Memory);
}
void BehaviorTreeCompoundNode::Init(BehaviorTree* tree)
{
for (BehaviorTreeNode* child : Children)
@@ -96,6 +102,20 @@ BehaviorUpdateResult BehaviorTreeCompoundNode::Update(BehaviorUpdateContext cont
return result;
}
void BehaviorTreeCompoundNode::InvokeReleaseState(const BehaviorUpdateContext& context)
{
const BitArray<>& relevantNodes = *(const BitArray<>*)context.RelevantNodes;
for (BehaviorTreeNode* child : Children)
{
if (relevantNodes.Get(child->_executionIndex) == true)
{
child->InvokeReleaseState(context);
}
}
BehaviorTreeNode::InvokeReleaseState(context);
}
int32 BehaviorTreeSequenceNode::GetStateSize() const
{
return sizeof(State);

View File

@@ -6,6 +6,7 @@
#include "BehaviorTreeNode.h"
#include "BehaviorKnowledgeSelector.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Core/Collections/BitArray.h"
#include "Engine/Content/AssetReference.h"
/// <summary>
@@ -24,6 +25,10 @@ public:
// [BehaviorTreeNode]
void Init(BehaviorTree* tree) override;
BehaviorUpdateResult Update(BehaviorUpdateContext context) override;
protected:
// [BehaviorTreeNode]
void InvokeReleaseState(const BehaviorUpdateContext& context) override;
};
/// <summary>