From d1e2d6699efce0622c07f1e54601d150fe354667 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 16 Aug 2023 22:28:48 +0200 Subject: [PATCH] Add bt nodes init --- Source/Engine/AI/BehaviorTree.cpp | 6 ++++++ Source/Engine/AI/BehaviorTreeNode.h | 9 ++++++++- Source/Engine/AI/BehaviorTreeNodes.cpp | 6 ++++++ Source/Engine/AI/BehaviorTreeNodes.h | 5 ++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Source/Engine/AI/BehaviorTree.cpp b/Source/Engine/AI/BehaviorTree.cpp index aea8105a9..03e663a64 100644 --- a/Source/Engine/AI/BehaviorTree.cpp +++ b/Source/Engine/AI/BehaviorTree.cpp @@ -146,6 +146,12 @@ Asset::LoadResult BehaviorTree::load() return LoadResult::Failed; } + // Init graph + if (Graph.Root) + { + Graph.Root->Init(this); + } + return LoadResult::Ok; } diff --git a/Source/Engine/AI/BehaviorTreeNode.h b/Source/Engine/AI/BehaviorTreeNode.h index 6e3e29495..ed9523eb1 100644 --- a/Source/Engine/AI/BehaviorTreeNode.h +++ b/Source/Engine/AI/BehaviorTreeNode.h @@ -19,11 +19,18 @@ public: API_FIELD() String Name; // TODO: decorators/conditionals - // TODO: init methods // TODO: instance data ctor/dtor // TODO: start/stop methods // TODO: update method + /// + /// Initializes node state. Called after whole tree is loaded and nodes hierarchy is setup. + /// + /// Node owner asset. + API_FUNCTION() virtual void Init(BehaviorTree* tree) + { + } + void Serialize(SerializeStream& stream, const void* otherObj) override; void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override; }; diff --git a/Source/Engine/AI/BehaviorTreeNodes.cpp b/Source/Engine/AI/BehaviorTreeNodes.cpp index 8cfcb4899..392eae3b2 100644 --- a/Source/Engine/AI/BehaviorTreeNodes.cpp +++ b/Source/Engine/AI/BehaviorTreeNodes.cpp @@ -18,3 +18,9 @@ void BehaviorTreeNode::Deserialize(DeserializeStream& stream, ISerializeModifier Name.Clear(); // Missing Name is assumes as unnamed node DESERIALIZE(Name); } + +void BehaviorTreeCompoundNode::Init(BehaviorTree* tree) +{ + for (BehaviorTreeNode* child : Children) + child->Init(tree); +} diff --git a/Source/Engine/AI/BehaviorTreeNodes.h b/Source/Engine/AI/BehaviorTreeNodes.h index 7a8561a45..2387206b2 100644 --- a/Source/Engine/AI/BehaviorTreeNodes.h +++ b/Source/Engine/AI/BehaviorTreeNodes.h @@ -12,11 +12,14 @@ API_CLASS(Abstract) class FLAXENGINE_API BehaviorTreeCompoundNode : public Behav { DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(BehaviorTreeCompoundNode, BehaviorTreeNode); -public: /// /// List with all child nodes. /// API_FIELD(Readonly) Array> Children; + +public: + // [BehaviorTreeNode] + void Init(BehaviorTree* tree) override; }; ///