From ef1c1e5192976a32619eb89577e89896e80498a7 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 14 May 2024 22:03:29 +0200 Subject: [PATCH] Fix circular ref & non-tools compilation errors --- bt/behavior_tree.cpp | 12 ++++++++++-- bt/behavior_tree.h | 3 +++ bt/tasks/bt_task.cpp | 12 +++++++++--- bt/tasks/bt_task.h | 2 +- bt/tasks/decorators/bt_new_scope.cpp | 2 ++ bt/tasks/decorators/bt_new_scope.h | 2 ++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/bt/behavior_tree.cpp b/bt/behavior_tree.cpp index 3482462..b15f480 100644 --- a/bt/behavior_tree.cpp +++ b/bt/behavior_tree.cpp @@ -52,9 +52,13 @@ void BehaviorTree::set_blackboard_plan(const Ref &p_plan) { } void BehaviorTree::set_root_task(const Ref &p_value) { +#ifdef TOOLS_ENABLED _unset_editor_behavior_tree_hint(); +#endif // TOOLS_ENABLED root_task = p_value; +#ifdef TOOLS_ENABLED _set_editor_behavior_tree_hint(); +#endif // TOOLS_ENABLED emit_changed(); } @@ -87,18 +91,22 @@ void BehaviorTree::_plan_changed() { emit_changed(); } +#ifdef TOOLS_ENABLED + void BehaviorTree::_set_editor_behavior_tree_hint() { if (root_task.is_valid()) { - root_task->data.behavior_tree = Ref(this); + root_task->data.behavior_tree_id = this->get_instance_id(); } } void BehaviorTree::_unset_editor_behavior_tree_hint() { if (root_task.is_valid()) { - root_task->data.behavior_tree.unref(); + root_task->data.behavior_tree_id = ObjectID(); } } +#endif // TOOLS_ENABLED + void BehaviorTree::_bind_methods() { ClassDB::bind_method(D_METHOD("set_description", "description"), &BehaviorTree::set_description); ClassDB::bind_method(D_METHOD("get_description"), &BehaviorTree::get_description); diff --git a/bt/behavior_tree.h b/bt/behavior_tree.h index 4d71b9d..46507c5 100644 --- a/bt/behavior_tree.h +++ b/bt/behavior_tree.h @@ -33,8 +33,11 @@ private: Ref root_task; void _plan_changed(); + +#ifdef TOOLS_ENABLED void _set_editor_behavior_tree_hint(); void _unset_editor_behavior_tree_hint(); +#endif // TOOLS_ENABLED protected: static void _bind_methods(); diff --git a/bt/tasks/bt_task.cpp b/bt/tasks/bt_task.cpp index 7bc317b..4fa7ef1 100644 --- a/bt/tasks/bt_task.cpp +++ b/bt/tasks/bt_task.cpp @@ -377,18 +377,22 @@ void BTTask::print_tree(int p_initial_tabs) { } } +#ifdef TOOLS_ENABLED + Ref BTTask::editor_get_behavior_tree() { BTTask *task = this; - while (task->data.behavior_tree.is_null() && task->get_parent().is_valid()) { + while (task->data.behavior_tree_id.is_null() && task->get_parent().is_valid()) { task = task->data.parent; } - return task->data.behavior_tree; + return Object::cast_to(ObjectDB::get_instance(task->data.behavior_tree_id)); } void BTTask::editor_set_behavior_tree(const Ref &p_bt) { - data.behavior_tree = p_bt; + data.behavior_tree_id = p_bt->get_instance_id(); } +#endif // TOOLS_ENABLED + void BTTask::_bind_methods() { // Public Methods. ClassDB::bind_method(D_METHOD("is_root"), &BTTask::is_root); @@ -410,7 +414,9 @@ void BTTask::_bind_methods() { ClassDB::bind_method(D_METHOD("print_tree", "initial_tabs"), &BTTask::print_tree, Variant(0)); ClassDB::bind_method(D_METHOD("get_task_name"), &BTTask::get_task_name); ClassDB::bind_method(D_METHOD("abort"), &BTTask::abort); +#ifdef TOOLS_ENABLED ClassDB::bind_method(D_METHOD("editor_get_behavior_tree"), &BTTask::editor_get_behavior_tree); +#endif // TOOLS_ENABLED // Properties, setters and getters. ClassDB::bind_method(D_METHOD("get_agent"), &BTTask::get_agent); diff --git a/bt/tasks/bt_task.h b/bt/tasks/bt_task.h index 446e36d..b571c27 100644 --- a/bt/tasks/bt_task.h +++ b/bt/tasks/bt_task.h @@ -85,7 +85,7 @@ private: double elapsed = 0.0; bool display_collapsed = false; #ifdef TOOLS_ENABLED - Ref behavior_tree; + ObjectID behavior_tree_id; #endif } data; diff --git a/bt/tasks/decorators/bt_new_scope.cpp b/bt/tasks/decorators/bt_new_scope.cpp index 48a3319..c9a85b5 100644 --- a/bt/tasks/decorators/bt_new_scope.cpp +++ b/bt/tasks/decorators/bt_new_scope.cpp @@ -30,12 +30,14 @@ void BTNewScope::set_blackboard_plan(const Ref &p_plan) { emit_changed(); } +#ifdef TOOLS_ENABLED void BTNewScope::_set_parent_scope_plan_from_bt() { ERR_FAIL_NULL(get_blackboard_plan()); Ref bt = get_root()->editor_get_behavior_tree(); ERR_FAIL_NULL(bt); get_blackboard_plan()->set_parent_scope_plan(bt->get_blackboard_plan()); } +#endif // TOOLS_ENABLED void BTNewScope::initialize(Node *p_agent, const Ref &p_blackboard, Node *p_scene_root) { ERR_FAIL_COND(p_agent == nullptr); diff --git a/bt/tasks/decorators/bt_new_scope.h b/bt/tasks/decorators/bt_new_scope.h index df2c505..b3662f5 100644 --- a/bt/tasks/decorators/bt_new_scope.h +++ b/bt/tasks/decorators/bt_new_scope.h @@ -23,7 +23,9 @@ class BTNewScope : public BTDecorator { private: Ref blackboard_plan; +#ifdef TOOLS_ENABLED void _set_parent_scope_plan_from_bt(); +#endif // TOOLS_ENABLED protected: static void _bind_methods();