From f588d6da51c218a70e2f3c8d9b11cca6b74242f6 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 2 Sep 2025 20:33:41 -0500 Subject: [PATCH 1/2] Add ability to loop root node in behavior trees. --- Source/Engine/AI/Behavior.cpp | 14 +++++++++++++- Source/Engine/AI/BehaviorTreeNodes.h | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Source/Engine/AI/Behavior.cpp b/Source/Engine/AI/Behavior.cpp index e70ca50a4..f0c4acee3 100644 --- a/Source/Engine/AI/Behavior.cpp +++ b/Source/Engine/AI/Behavior.cpp @@ -106,7 +106,19 @@ void Behavior::UpdateAsync() const BehaviorUpdateResult result = tree->Graph.Root->InvokeUpdate(context); if (result != BehaviorUpdateResult::Running) _result = result; - if (_result != BehaviorUpdateResult::Running) + if (_result != BehaviorUpdateResult::Running && tree->Graph.Root->Loop) + { + // Ensure to have tree loaded on play + CHECK(Tree && !Tree->WaitForLoaded()); + BehaviorTree* tree = Tree.Get(); + CHECK(tree->Graph.Root); + + // Setup state + _result = BehaviorUpdateResult::Running; + _accumulatedTime = 0.0f; + _totalTime = 0; + } + else if (_result != BehaviorUpdateResult::Running) { Finished(); } diff --git a/Source/Engine/AI/BehaviorTreeNodes.h b/Source/Engine/AI/BehaviorTreeNodes.h index 7aaab5f3c..05a472da7 100644 --- a/Source/Engine/AI/BehaviorTreeNodes.h +++ b/Source/Engine/AI/BehaviorTreeNodes.h @@ -96,6 +96,10 @@ API_CLASS(Sealed) class FLAXENGINE_API BehaviorTreeRootNode : public BehaviorTre // The target amount of the behavior logic updates per second. API_FIELD(Attributes="EditorOrder(100)") float UpdateFPS = 10.0f; + + // Whether to loop the root node. + API_FIELD(Attributes="EditorOrder(200)") + bool Loop = true; }; /// From cd0878f81064791f4f94c9faee8c47c6e1af4ead Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 2 Sep 2025 20:47:04 -0500 Subject: [PATCH 2/2] Simplify code --- Source/Engine/AI/Behavior.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Source/Engine/AI/Behavior.cpp b/Source/Engine/AI/Behavior.cpp index f0c4acee3..d727bdef1 100644 --- a/Source/Engine/AI/Behavior.cpp +++ b/Source/Engine/AI/Behavior.cpp @@ -108,12 +108,7 @@ void Behavior::UpdateAsync() _result = result; if (_result != BehaviorUpdateResult::Running && tree->Graph.Root->Loop) { - // Ensure to have tree loaded on play - CHECK(Tree && !Tree->WaitForLoaded()); - BehaviorTree* tree = Tree.Get(); - CHECK(tree->Graph.Root); - - // Setup state + // Reset State _result = BehaviorUpdateResult::Running; _accumulatedTime = 0.0f; _totalTime = 0;