From 1b9cf1733948a31149846c3e3749d1fc8ae03ba8 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 21 Jan 2025 23:35:28 +0100 Subject: [PATCH] Fix BTPlayer.set_behavior_tree() not populating Blackboard (#274) --- bt/bt_instance.cpp | 3 ++- bt/bt_player.cpp | 29 +++++++++++++++++------------ bt/bt_player.h | 3 ++- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/bt/bt_instance.cpp b/bt/bt_instance.cpp index 05db482..1abcc8b 100644 --- a/bt/bt_instance.cpp +++ b/bt/bt_instance.cpp @@ -83,7 +83,7 @@ void BTInstance::register_with_debugger() { void BTInstance::unregister_with_debugger() { #ifdef DEBUG_ENABLED - if (LimboDebugger::get_singleton()->is_active()) { + if (LimboDebugger::get_singleton() && LimboDebugger::get_singleton()->is_active()) { LimboDebugger::get_singleton()->unregister_bt_instance(get_instance_id()); } #endif @@ -151,5 +151,6 @@ BTInstance::~BTInstance() { emit_signal(LW_NAME(freed)); #ifdef DEBUG_ENABLED _remove_custom_monitor(); + unregister_with_debugger(); #endif } diff --git a/bt/bt_player.cpp b/bt/bt_player.cpp index 8d36eab..4a1790d 100644 --- a/bt/bt_player.cpp +++ b/bt/bt_player.cpp @@ -42,7 +42,7 @@ VARIANT_ENUM_CAST(BTPlayer::UpdateMode); -void BTPlayer::_load_tree() { +void BTPlayer::_instantiate_bt() { bt_instance.unref(); ERR_FAIL_COND_MSG(!behavior_tree.is_valid(), "BTPlayer: Initialization failed - needs a valid behavior tree."); ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "BTPlayer: Initialization failed - behavior tree has no valid root task."); @@ -70,6 +70,19 @@ void BTPlayer::_update_blackboard_plan() { blackboard_plan->set_base_plan(behavior_tree.is_valid() ? behavior_tree->get_blackboard_plan() : nullptr); } +void BTPlayer::_initialize() { + if (blackboard.is_null()) { + blackboard = Ref(memnew(Blackboard)); + } + if (blackboard_plan.is_valid()) { + // Don't overwrite existing blackboard values as they may be initialized from code. + blackboard_plan->populate_blackboard(blackboard, false, this, _get_scene_root()); + } + if (behavior_tree.is_valid()) { + _instantiate_bt(); + } +} + void BTPlayer::set_bt_instance(const Ref &p_bt_instance) { ERR_FAIL_COND_MSG(p_bt_instance.is_null(), "BTPlayer: Failed to set behavior tree instance - instance is null."); ERR_FAIL_COND_MSG(!p_bt_instance->is_instance_valid(), "BTPlayer: Failed to set behavior tree instance - instance is not valid."); @@ -104,7 +117,8 @@ void BTPlayer::set_behavior_tree(const Ref &p_tree) { } else { behavior_tree = p_tree; if (get_owner() && is_inside_tree()) { - _load_tree(); + _update_blackboard_plan(); + _initialize(); } } } @@ -179,16 +193,7 @@ void BTPlayer::_notification(int p_notification) { } break; case NOTIFICATION_READY: { if (!Engine::get_singleton()->is_editor_hint()) { - if (blackboard.is_null()) { - blackboard = Ref(memnew(Blackboard)); - } - if (blackboard_plan.is_valid()) { - // Don't overwrite existing blackboard values as they may be initialized from code. - blackboard_plan->populate_blackboard(blackboard, false, this, _get_scene_root()); - } - if (behavior_tree.is_valid()) { - _load_tree(); - } + _initialize(); } else { _update_blackboard_plan(); } diff --git a/bt/bt_player.h b/bt/bt_player.h index 7d2ad6e..1644231 100644 --- a/bt/bt_player.h +++ b/bt/bt_player.h @@ -48,8 +48,9 @@ private: Ref bt_instance; - void _load_tree(); + void _instantiate_bt(); void _update_blackboard_plan(); + void _initialize(); _FORCE_INLINE_ Node *_get_scene_root() const { return scene_root_hint ? scene_root_hint : get_owner(); } protected: