From a572613001b75daaef2cdab490aaf63bd63a0c1f Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Wed, 15 May 2024 11:38:53 +0200 Subject: [PATCH] BlackboardPlan: Utilize mapping in LimboHSM Also changes how parent plan providing is implemented (used for editor hints). --- blackboard/blackboard_plan.cpp | 8 ++++---- blackboard/blackboard_plan.h | 13 +++++++------ bt/tasks/decorators/bt_new_scope.cpp | 2 +- editor/editor_property_variable_name.cpp | 7 ++++++- hsm/limbo_state.cpp | 19 +++++++++++++++++++ hsm/limbo_state.h | 2 ++ 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/blackboard/blackboard_plan.cpp b/blackboard/blackboard_plan.cpp index 47e9971..2756c8b 100644 --- a/blackboard/blackboard_plan.cpp +++ b/blackboard/blackboard_plan.cpp @@ -178,8 +178,8 @@ void BlackboardPlan::set_base_plan(const Ref &p_base) { notify_property_list_changed(); } -void BlackboardPlan::set_parent_scope_plan(const Ref &p_plan) { - parent_scope_plan = p_plan; +void BlackboardPlan::set_parent_scope_plan_provider(const Callable &p_parent_scope_plan_provider) { + parent_scope_plan_provider = p_parent_scope_plan_provider; notify_property_list_changed(); } @@ -422,8 +422,8 @@ void BlackboardPlan::_bind_methods() { ClassDB::bind_method(D_METHOD("set_base_plan", "blackboard_plan"), &BlackboardPlan::set_base_plan); ClassDB::bind_method(D_METHOD("get_base_plan"), &BlackboardPlan::get_base_plan); - ClassDB::bind_method(D_METHOD("set_parent_scope_plan", "blackboard_plan"), &BlackboardPlan::set_parent_scope_plan); - ClassDB::bind_method(D_METHOD("get_parent_scope_plan"), &BlackboardPlan::get_parent_scope_plan); + ClassDB::bind_method(D_METHOD("set_parent_scope_plan_provider", "callable"), &BlackboardPlan::set_parent_scope_plan_provider); + ClassDB::bind_method(D_METHOD("get_parent_scope_plan_provider"), &BlackboardPlan::get_parent_scope_plan_provider); ClassDB::bind_method(D_METHOD("is_mapping_enabled"), &BlackboardPlan::is_mapping_enabled); ClassDB::bind_method(D_METHOD("is_derived"), &BlackboardPlan::is_derived); ClassDB::bind_method(D_METHOD("sync_with_base_plan"), &BlackboardPlan::sync_with_base_plan); diff --git a/blackboard/blackboard_plan.h b/blackboard/blackboard_plan.h index d24edab..21ccbbf 100644 --- a/blackboard/blackboard_plan.h +++ b/blackboard/blackboard_plan.h @@ -39,9 +39,9 @@ private: // Mapping between variables in this plan and their parent scope names. // Used for linking variables to their parent scope counterparts upon Blackboard creation/population. HashMap parent_scope_mapping; - // BlackboardPlan that will be used in parent scope Blackboard creation at runtime. - // Used to provide hints in the inspector. When set, mapping is enabled in the inspector. - Ref parent_scope_plan; + // Fetcher function for the parent scope plan. Funtion should return a Ref. + // Used in the inspector. When set, mapping feature becomes available. + Callable parent_scope_plan_provider; // If true, NodePath variables will be prefetched, so that the vars will contain node pointers instead (upon BB creation/population). bool prefetch_nodepath_vars = true; @@ -59,9 +59,10 @@ public: void set_base_plan(const Ref &p_base); Ref get_base_plan() const { return base; } - void set_parent_scope_plan(const Ref &p_plan); - Ref get_parent_scope_plan() const { return parent_scope_plan; } - bool is_mapping_enabled() const { return parent_scope_plan.is_valid(); } + void set_parent_scope_plan_provider(const Callable &p_parent_scope_plan_provider); + Callable get_parent_scope_plan_provider() const { return parent_scope_plan_provider; } + + bool is_mapping_enabled() const { return parent_scope_plan_provider.is_valid(); } bool has_mapping(const StringName &p_name) const; void set_prefetch_nodepath_vars(bool p_enable); diff --git a/bt/tasks/decorators/bt_new_scope.cpp b/bt/tasks/decorators/bt_new_scope.cpp index c9a85b5..5a2b1c4 100644 --- a/bt/tasks/decorators/bt_new_scope.cpp +++ b/bt/tasks/decorators/bt_new_scope.cpp @@ -35,7 +35,7 @@ void BTNewScope::_set_parent_scope_plan_from_bt() { ERR_FAIL_NULL(get_blackboard_plan()); Ref bt = get_root()->editor_get_behavior_tree(); ERR_FAIL_NULL(bt); - get_blackboard_plan()->set_parent_scope_plan(bt->get_blackboard_plan()); + get_blackboard_plan()->set_parent_scope_plan_provider(callable_mp(bt.ptr(), &BehaviorTree::get_blackboard_plan)); } #endif // TOOLS_ENABLED diff --git a/editor/editor_property_variable_name.cpp b/editor/editor_property_variable_name.cpp index 3e23977..939c5d9 100644 --- a/editor/editor_property_variable_name.cpp +++ b/editor/editor_property_variable_name.cpp @@ -241,7 +241,12 @@ bool EditorInspectorPluginVariableName::_parse_property(Object *p_object, const expected_hint = variable.get_hint(); expected_hint_string = variable.get_hint_string(); } - plan = plan->get_parent_scope_plan(); + if (plan->get_parent_scope_plan_provider().is_valid()) { + Ref parent_plan = plan->get_parent_scope_plan_provider().call(); + if (parent_plan.is_valid()) { + plan = parent_plan; + } + } ERR_FAIL_NULL_V(plan, false); } else { plan = plan_getter.call(); diff --git a/hsm/limbo_state.cpp b/hsm/limbo_state.cpp index 77554bb..f704dc1 100644 --- a/hsm/limbo_state.cpp +++ b/hsm/limbo_state.cpp @@ -22,12 +22,31 @@ void LimboState::set_blackboard_plan(const Ref &p_plan) { blackboard_plan = p_plan; + + if (Engine::get_singleton()->is_editor_hint() && blackboard_plan.is_valid()) { + blackboard_plan->set_parent_scope_plan_provider(callable_mp(this, &LimboState::_get_parent_scope_plan)); + } + _update_blackboard_plan(); } void LimboState::_update_blackboard_plan() { } +Ref LimboState::_get_parent_scope_plan() const { + BlackboardPlan *parent_plan = nullptr; + const LimboState *state = this; + while (state->get_parent() && IS_CLASS(state->get_parent(), LimboState)) { + state = Object::cast_to(state->get_parent()); + ERR_FAIL_NULL_V(state, parent_plan); + if (state->blackboard_plan.is_valid()) { + parent_plan = state->blackboard_plan.ptr(); + break; + } + } + return parent_plan; +} + LimboState *LimboState::get_root() const { const Node *state = this; while (state->get_parent() && IS_CLASS(state->get_parent(), LimboState)) { diff --git a/hsm/limbo_state.h b/hsm/limbo_state.h index 69bc2cf..a4d9b00 100644 --- a/hsm/limbo_state.h +++ b/hsm/limbo_state.h @@ -38,6 +38,8 @@ private: HashMap handlers; Callable guard_callable; + Ref _get_parent_scope_plan() const; + protected: friend LimboHSM;