Skip to content

Commit

Permalink
Fix BTPlayer.set_behavior_tree() not populating Blackboard (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
limbonaut authored Jan 21, 2025
1 parent 0584b04 commit 1b9cf17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion bt/bt_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -151,5 +151,6 @@ BTInstance::~BTInstance() {
emit_signal(LW_NAME(freed));
#ifdef DEBUG_ENABLED
_remove_custom_monitor();
unregister_with_debugger();
#endif
}
29 changes: 17 additions & 12 deletions bt/bt_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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<Blackboard>(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<BTInstance> &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.");
Expand Down Expand Up @@ -104,7 +117,8 @@ void BTPlayer::set_behavior_tree(const Ref<BehaviorTree> &p_tree) {
} else {
behavior_tree = p_tree;
if (get_owner() && is_inside_tree()) {
_load_tree();
_update_blackboard_plan();
_initialize();
}
}
}
Expand Down Expand Up @@ -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<Blackboard>(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();
}
Expand Down
3 changes: 2 additions & 1 deletion bt/bt_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class BTPlayer : public Node {

Ref<BTInstance> 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:
Expand Down

0 comments on commit 1b9cf17

Please sign in to comment.