From 39182722272566ce8486e6619c87a65e0b084afb Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 1 Apr 2024 16:34:36 +0200 Subject: [PATCH] BehaviorTree: New signal for when BB plan changes Utilize `plan_changed` signal in `BTPlayer` and `BTState`. --- bt/behavior_tree.cpp | 5 ++++- bt/bt_player.cpp | 21 ++++++++++----------- bt/bt_state.cpp | 10 +++++----- doc/source/classes/class_behaviortree.rst | 17 +++++++++++++++++ doc_classes/BehaviorTree.xml | 7 +++++++ util/limbo_string_names.cpp | 1 + util/limbo_string_names.h | 1 + 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/bt/behavior_tree.cpp b/bt/behavior_tree.cpp index f5bfc1d..4b5e5f0 100644 --- a/bt/behavior_tree.cpp +++ b/bt/behavior_tree.cpp @@ -48,7 +48,7 @@ void BehaviorTree::set_blackboard_plan(const Ref &p_plan) { blackboard_plan->connect(LW_NAME(changed), callable_mp(this, &BehaviorTree::_plan_changed)); } - emit_changed(); + _plan_changed(); } void BehaviorTree::set_root_task(const Ref &p_value) { @@ -79,6 +79,7 @@ Ref BehaviorTree::instantiate(Node *p_agent, const Ref &p_bl } void BehaviorTree::_plan_changed() { + emit_signal(LW_NAME(plan_changed)); emit_changed(); } @@ -96,6 +97,8 @@ void BehaviorTree::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "description", PROPERTY_HINT_MULTILINE_TEXT), "set_description", "get_description"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard_plan", PROPERTY_HINT_RESOURCE_TYPE, "BlackboardPlan", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_blackboard_plan", "get_blackboard_plan"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_root_task", "get_root_task"); + + ADD_SIGNAL(MethodInfo("plan_changed")); } BehaviorTree::BehaviorTree() { diff --git a/bt/bt_player.cpp b/bt/bt_player.cpp index 7840400..626962a 100644 --- a/bt/bt_player.cpp +++ b/bt/bt_player.cpp @@ -63,17 +63,21 @@ void BTPlayer::_load_tree() { void BTPlayer::_update_blackboard_plan() { if (blackboard_plan.is_null()) { blackboard_plan = Ref(memnew(BlackboardPlan)); + } else if (!RESOURCE_IS_BUILT_IN(blackboard_plan)) { + WARN_PRINT_ED("BTPlayer: Using external resource for derived blackboard plan is not supported. Converted to built-in resource."); + blackboard_plan = blackboard_plan->duplicate(); } + blackboard_plan->set_base_plan(behavior_tree.is_valid() ? behavior_tree->get_blackboard_plan() : nullptr); } void BTPlayer::set_behavior_tree(const Ref &p_tree) { if (Engine::get_singleton()->is_editor_hint()) { - if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(changed), callable_mp(this, &BTPlayer::_update_blackboard_plan))) { - behavior_tree->disconnect(LW_NAME(changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); + if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(plan_changed), callable_mp(this, &BTPlayer::_update_blackboard_plan))) { + behavior_tree->disconnect(LW_NAME(plan_changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); } if (p_tree.is_valid()) { - p_tree->connect(LW_NAME(changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); + p_tree->connect(LW_NAME(plan_changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); } behavior_tree = p_tree; _update_blackboard_plan(); @@ -86,12 +90,7 @@ void BTPlayer::set_behavior_tree(const Ref &p_tree) { } void BTPlayer::set_blackboard_plan(const Ref &p_plan) { - if (p_plan.is_valid() && !RESOURCE_IS_BUILT_IN(p_plan)) { - WARN_PRINT_ED("BTPlayer: Using external resource for derived blackboard plan is not supported. Converted to built-in resource."); - blackboard_plan = p_plan->duplicate(); - } else { - blackboard_plan = p_plan; - } + blackboard_plan = p_plan; _update_blackboard_plan(); } @@ -217,8 +216,8 @@ void BTPlayer::_notification(int p_notification) { #endif // DEBUG_ENABLED if (Engine::get_singleton()->is_editor_hint()) { - if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(changed), callable_mp(this, &BTPlayer::_update_blackboard_plan))) { - behavior_tree->disconnect(LW_NAME(changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); + if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(plan_changed), callable_mp(this, &BTPlayer::_update_blackboard_plan))) { + behavior_tree->disconnect(LW_NAME(plan_changed), callable_mp(this, &BTPlayer::_update_blackboard_plan)); } } diff --git a/bt/bt_state.cpp b/bt/bt_state.cpp index ec1c64f..8497408 100644 --- a/bt/bt_state.cpp +++ b/bt/bt_state.cpp @@ -25,11 +25,11 @@ void BTState::set_behavior_tree(const Ref &p_tree) { if (Engine::get_singleton()->is_editor_hint()) { - if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan))) { - behavior_tree->disconnect(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan)); + if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(plan_changed), callable_mp(this, &BTState::_update_blackboard_plan))) { + behavior_tree->disconnect(LW_NAME(plan_changed), callable_mp(this, &BTState::_update_blackboard_plan)); } if (p_tree.is_valid()) { - p_tree->connect(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan)); + p_tree->connect(LW_NAME(plan_changed), callable_mp(this, &BTState::_update_blackboard_plan)); } behavior_tree = p_tree; _update_blackboard_plan(); @@ -97,8 +97,8 @@ void BTState::_notification(int p_notification) { #endif // DEBUG_ENABLED if (Engine::get_singleton()->is_editor_hint()) { - if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan))) { - behavior_tree->disconnect(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan)); + if (behavior_tree.is_valid() && behavior_tree->is_connected(LW_NAME(plan_changed), callable_mp(this, &BTState::_update_blackboard_plan))) { + behavior_tree->disconnect(LW_NAME(plan_changed), callable_mp(this, &BTState::_update_blackboard_plan)); } } } break; diff --git a/doc/source/classes/class_behaviortree.rst b/doc/source/classes/class_behaviortree.rst index a5b153f..b91f662 100644 --- a/doc/source/classes/class_behaviortree.rst +++ b/doc/source/classes/class_behaviortree.rst @@ -73,6 +73,23 @@ Methods .. rst-class:: classref-descriptions-group +Signals +------- + +.. _class_BehaviorTree_signal_plan_changed: + +.. rst-class:: classref-signal + +**plan_changed** **(** **)** + +Emitted when the :ref:`BlackboardPlan` changes. + +.. rst-class:: classref-section-separator + +---- + +.. rst-class:: classref-descriptions-group + Property Descriptions --------------------- diff --git a/doc_classes/BehaviorTree.xml b/doc_classes/BehaviorTree.xml index d36063f..e5f9645 100644 --- a/doc_classes/BehaviorTree.xml +++ b/doc_classes/BehaviorTree.xml @@ -58,4 +58,11 @@ User-provided description of the BehaviorTree. + + + + Emitted when the [BlackboardPlan] changes. + + + diff --git a/util/limbo_string_names.cpp b/util/limbo_string_names.cpp index c1ad2d6..0f1dd67 100644 --- a/util/limbo_string_names.cpp +++ b/util/limbo_string_names.cpp @@ -122,6 +122,7 @@ LimboStringNames::LimboStringNames() { NonFavorite = SN("NonFavorite"); normal = SN("normal"); panel = SN("panel"); + plan_changed = SN("plan_changed"); popup_hide = SN("popup_hide"); pressed = SN("pressed"); probability_clicked = SN("probability_clicked"); diff --git a/util/limbo_string_names.h b/util/limbo_string_names.h index 3814785..fe62c27 100644 --- a/util/limbo_string_names.h +++ b/util/limbo_string_names.h @@ -137,6 +137,7 @@ public: StringName NonFavorite; StringName normal; StringName panel; + StringName plan_changed; StringName popup_hide; StringName pressed; StringName probability_clicked;