Skip to content

Commit 5766922

Browse files
committed
Use different prefetch root for the base plan in BTPlayer and BTState
(cherry picked from commit ac2d734)
1 parent 65a1a3d commit 5766922

File tree

7 files changed

+18
-5
lines changed

7 files changed

+18
-5
lines changed

blackboard/blackboard_plan.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void BlackboardPlan::populate_blackboard(const Ref<Blackboard> &p_blackboard, bo
416416
// Add a variable duplicate to the blackboard, optionally with NodePath prefetch.
417417
BBVariable var = p.second.duplicate(true);
418418
if (unlikely(do_prefetch && p.second.get_type() == Variant::NODE_PATH)) {
419-
Node *prefetch_root = !is_derived() || is_derived_var_changed(p.first) ? p_prefetch_root : p_prefetch_root_for_base_plan;
419+
Node *prefetch_root = !p_prefetch_root_for_base_plan || !is_derived() || is_derived_var_changed(p.first) ? p_prefetch_root : p_prefetch_root_for_base_plan;
420420
Node *n = prefetch_root->get_node_or_null(p.second.get_value());
421421
if (n != nullptr) {
422422
var.set_value(n);

bt/bt_player.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void BTPlayer::_load_tree() {
4848
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "BTPlayer: Initialization failed - behavior tree has no valid root task.");
4949
Node *agent = GET_NODE(this, agent_node);
5050
ERR_FAIL_NULL_MSG(agent, vformat("BTPlayer: Initialization failed - can't get agent with path '%s'.", agent_node));
51-
Node *scene_root = scene_root_hint ? scene_root_hint : get_owner();
51+
Node *scene_root = _get_scene_root();
5252
ERR_FAIL_COND_MSG(scene_root == nullptr,
5353
"BTPlayer: Initialization failed - unable to establish scene root. This is likely due to BTPlayer not being owned by a scene node. Check BTPlayer.set_scene_root_hint().");
5454
bt_instance = behavior_tree->instantiate(agent, blackboard, this, scene_root);
@@ -184,7 +184,7 @@ void BTPlayer::_notification(int p_notification) {
184184
}
185185
if (blackboard_plan.is_valid()) {
186186
// Don't overwrite existing blackboard values as they may be initialized from code.
187-
blackboard_plan->populate_blackboard(blackboard, false, this);
187+
blackboard_plan->populate_blackboard(blackboard, false, this, _get_scene_root());
188188
}
189189
if (behavior_tree.is_valid()) {
190190
_load_tree();

bt/bt_player.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class BTPlayer : public Node {
5050

5151
void _load_tree();
5252
void _update_blackboard_plan();
53+
_FORCE_INLINE_ Node *_get_scene_root() const { return scene_root_hint ? scene_root_hint : get_owner(); }
5354

5455
protected:
5556
static void _bind_methods();

bt/bt_state.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ void BTState::_update_blackboard_plan() {
6565
}
6666
}
6767

68+
Node *BTState::_get_prefetch_root_for_base_plan() {
69+
return _get_scene_root();
70+
}
71+
6872
void BTState::_setup() {
6973
LimboState::_setup();
7074
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
71-
Node *scene_root = scene_root_hint ? scene_root_hint : get_owner();
75+
Node *scene_root = _get_scene_root();
7276
ERR_FAIL_NULL_MSG(scene_root, "BTState: Initialization failed - unable to establish scene root. This is likely due to BTState not being owned by a scene node. Check BTState.set_scene_root_hint().");
7377
bt_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), this, scene_root);
7478
ERR_FAIL_COND_MSG(bt_instance.is_null(), "BTState: Initialization failed - failed to instantiate behavior tree.");

bt/bt_state.h

+3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ class BTState : public LimboState {
2828
Node *scene_root_hint = nullptr;
2929
bool monitor_performance = false;
3030

31+
_FORCE_INLINE_ Node *_get_scene_root() const { return scene_root_hint ? scene_root_hint : get_owner(); }
32+
3133
protected:
3234
static void _bind_methods();
3335

3436
void _notification(int p_notification);
3537

3638
virtual bool _should_use_new_scope() const override { return true; }
3739
virtual void _update_blackboard_plan() override;
40+
virtual Node *_get_prefetch_root_for_base_plan() override;
3841

3942
virtual void _setup() override;
4043
virtual void _exit() override;

hsm/limbo_state.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ void LimboState::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
3333
void LimboState::_update_blackboard_plan() {
3434
}
3535

36+
Node *LimboState::_get_prefetch_root_for_base_plan() {
37+
return this;
38+
}
39+
3640
Ref<BlackboardPlan> LimboState::_get_parent_scope_plan() const {
3741
BlackboardPlan *parent_plan = nullptr;
3842
const LimboState *state = this;
@@ -96,7 +100,7 @@ void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
96100
}
97101
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
98102
// Don't overwrite existing blackboard values as they may be initialized from code.
99-
blackboard_plan->populate_blackboard(blackboard, false, this);
103+
blackboard_plan->populate_blackboard(blackboard, false, this, _get_prefetch_root_for_base_plan());
100104
}
101105

102106
_setup();

hsm/limbo_state.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class LimboState : public Node {
5454

5555
virtual bool _should_use_new_scope() const { return blackboard_plan.is_valid() || is_root(); }
5656
virtual void _update_blackboard_plan();
57+
virtual Node *_get_prefetch_root_for_base_plan();
5758

5859
virtual void _setup();
5960
virtual void _enter();

0 commit comments

Comments
 (0)