Add Force Finish node to BT

This commit is contained in:
Wojtek Figat
2023-08-22 11:12:05 +02:00
parent d86eb5a4c2
commit 3259af3368
4 changed files with 29 additions and 5 deletions

View File

@@ -26,12 +26,12 @@ void Behavior::StartLogic()
_knowledge.InitMemory(tree);
}
void Behavior::StopLogic()
void Behavior::StopLogic(BehaviorUpdateResult result)
{
if (_result != BehaviorUpdateResult::Running)
if (_result != BehaviorUpdateResult::Running || result == BehaviorUpdateResult::Running)
return;
_accumulatedTime = 0.0f;
_result = BehaviorUpdateResult::Success;
_result = result;
}
void Behavior::ResetLogic()
@@ -83,8 +83,9 @@ void Behavior::OnLateUpdate()
context.DeltaTime = updateDeltaTime;
const BehaviorUpdateResult result = tree->Graph.Root->InvokeUpdate(context);
if (result != BehaviorUpdateResult::Running)
{
_result = result;
if (_result != BehaviorUpdateResult::Running)
{
Finished();
}
}

View File

@@ -71,7 +71,8 @@ public:
/// <summary>
/// Stops the logic.
/// </summary>
API_FUNCTION() void StopLogic();
/// <param name="result">The logic result.</param>
API_FUNCTION() void StopLogic(BehaviorUpdateResult result = BehaviorUpdateResult::Success);
/// <summary>
/// Resets the behavior logic by clearing knowledge (clears blackboard and removes goals) and resetting execution state (goes back to root).

View File

@@ -246,3 +246,9 @@ BehaviorUpdateResult BehaviorTreeSubTreeNode::Update(BehaviorUpdateContext conte
// Run nested tree
return tree->Graph.Root->InvokeUpdate(context);
}
BehaviorUpdateResult BehaviorTreeForceFinishNode::Update(BehaviorUpdateContext context)
{
context.Behavior->StopLogic(Result);
return Result;
}

View File

@@ -141,3 +141,19 @@ public:
BitArray<> RelevantNodes;
};
};
/// <summary>
/// Forces behavior execution end with a specific result (eg. force fail).
/// </summary>
API_CLASS(Sealed) class FLAXENGINE_API BehaviorTreeForceFinishNode : public BehaviorTreeNode
{
DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(BehaviorTreeForceFinishNode, BehaviorTreeNode);
API_AUTO_SERIALIZATION();
// Execution result.
API_FIELD() BehaviorUpdateResult Result = BehaviorUpdateResult::Success;
public:
// [BehaviorTreeNode]
BehaviorUpdateResult Update(BehaviorUpdateContext context) override;
};