Use different prefetch root for the base plan in BTPlayer and BTState

This commit is contained in:
Serhii Snitsaruk 2024-09-15 14:29:23 +02:00
parent a8a0f24492
commit ac2d734122
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
7 changed files with 18 additions and 5 deletions

View File

@ -416,7 +416,7 @@ void BlackboardPlan::populate_blackboard(const Ref<Blackboard> &p_blackboard, bo
// Add a variable duplicate to the blackboard, optionally with NodePath prefetch.
BBVariable var = p.second.duplicate(true);
if (unlikely(do_prefetch && p.second.get_type() == Variant::NODE_PATH)) {
Node *prefetch_root = !is_derived() || is_derived_var_changed(p.first) ? p_prefetch_root : p_prefetch_root_for_base_plan;
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;
Node *n = prefetch_root->get_node_or_null(p.second.get_value());
if (n != nullptr) {
var.set_value(n);

View File

@ -48,7 +48,7 @@ void BTPlayer::_load_tree() {
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "BTPlayer: Initialization failed - behavior tree has no valid root task.");
Node *agent = GET_NODE(this, agent_node);
ERR_FAIL_NULL_MSG(agent, vformat("BTPlayer: Initialization failed - can't get agent with path '%s'.", agent_node));
Node *scene_root = scene_root_hint ? scene_root_hint : get_owner();
Node *scene_root = _get_scene_root();
ERR_FAIL_COND_MSG(scene_root == nullptr,
"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().");
bt_instance = behavior_tree->instantiate(agent, blackboard, this, scene_root);
@ -184,7 +184,7 @@ void BTPlayer::_notification(int p_notification) {
}
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);
blackboard_plan->populate_blackboard(blackboard, false, this, _get_scene_root());
}
if (behavior_tree.is_valid()) {
_load_tree();

View File

@ -50,6 +50,7 @@ private:
void _load_tree();
void _update_blackboard_plan();
_FORCE_INLINE_ Node *_get_scene_root() const { return scene_root_hint ? scene_root_hint : get_owner(); }
protected:
static void _bind_methods();

View File

@ -65,10 +65,14 @@ void BTState::_update_blackboard_plan() {
}
}
Node *BTState::_get_prefetch_root_for_base_plan() {
return _get_scene_root();
}
void BTState::_setup() {
LimboState::_setup();
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
Node *scene_root = scene_root_hint ? scene_root_hint : get_owner();
Node *scene_root = _get_scene_root();
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().");
bt_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), this, scene_root);
ERR_FAIL_COND_MSG(bt_instance.is_null(), "BTState: Initialization failed - failed to instantiate behavior tree.");

View File

@ -28,6 +28,8 @@ private:
Node *scene_root_hint = nullptr;
bool monitor_performance = false;
_FORCE_INLINE_ Node *_get_scene_root() const { return scene_root_hint ? scene_root_hint : get_owner(); }
protected:
static void _bind_methods();
@ -35,6 +37,7 @@ protected:
virtual bool _should_use_new_scope() const override { return true; }
virtual void _update_blackboard_plan() override;
virtual Node *_get_prefetch_root_for_base_plan() override;
virtual void _setup() override;
virtual void _exit() override;

View File

@ -33,6 +33,10 @@ void LimboState::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
void LimboState::_update_blackboard_plan() {
}
Node *LimboState::_get_prefetch_root_for_base_plan() {
return this;
}
Ref<BlackboardPlan> LimboState::_get_parent_scope_plan() const {
BlackboardPlan *parent_plan = nullptr;
const LimboState *state = this;
@ -96,7 +100,7 @@ void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
}
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
// Don't overwrite existing blackboard values as they may be initialized from code.
blackboard_plan->populate_blackboard(blackboard, false, this);
blackboard_plan->populate_blackboard(blackboard, false, this, _get_prefetch_root_for_base_plan());
}
_setup();

View File

@ -54,6 +54,7 @@ protected:
virtual bool _should_use_new_scope() const { return blackboard_plan.is_valid() || is_root(); }
virtual void _update_blackboard_plan();
virtual Node *_get_prefetch_root_for_base_plan();
virtual void _setup();
virtual void _enter();