From 434545ebad578e0f00ef16a21f17b688aacce7f2 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 12 Mar 2024 16:17:34 +0100 Subject: [PATCH] Blackboard: Linking vars with `Blackboard::link_var` --- blackboard/blackboard.cpp | 8 ++++ blackboard/blackboard.h | 2 + doc/source/classes/class_blackboard.rst | 56 ++++++++++++++++--------- doc_classes/Blackboard.xml | 11 +++++ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/blackboard/blackboard.cpp b/blackboard/blackboard.cpp index db43265..af55d89 100644 --- a/blackboard/blackboard.cpp +++ b/blackboard/blackboard.cpp @@ -78,6 +78,13 @@ void Blackboard::assign_var(const StringName &p_name, const BBVariable &p_var) { data.insert(p_name, p_var); } +void Blackboard::link_var(const StringName &p_name, const Ref &p_target_blackboard, const StringName &p_target_var) { + ERR_FAIL_COND_MSG(!data.has(p_name), "Blackboard: Can't link variable that doesn't exist (var: " + p_name + ")."); + ERR_FAIL_COND_MSG(p_target_blackboard.is_null(), "Blackboard: Can't link variable to target blackboard that is null (var: " + p_name + ")."); + ERR_FAIL_COND_MSG(!p_target_blackboard->data.has(p_target_var), "Blackboard: Can't link variable to non-existent target (var: " + p_name + ", target: " + p_target_var + ")."); + data[p_name] = p_target_blackboard->data[p_target_var]; +} + void Blackboard::_bind_methods() { ClassDB::bind_method(D_METHOD("get_var", "var_name", "default", "complain"), &Blackboard::get_var, Variant(), true); ClassDB::bind_method(D_METHOD("set_var", "var_name", "value"), &Blackboard::set_var); @@ -88,4 +95,5 @@ void Blackboard::_bind_methods() { ClassDB::bind_method(D_METHOD("top"), &Blackboard::top); ClassDB::bind_method(D_METHOD("bind_var_to_property", "var_name", "object", "property"), &Blackboard::bind_var_to_property); ClassDB::bind_method(D_METHOD("unbind_var", "var_name"), &Blackboard::unbind_var); + ClassDB::bind_method(D_METHOD("link_var", "var_name", "target_blackboard", "target_var"), &Blackboard::link_var); } diff --git a/blackboard/blackboard.h b/blackboard/blackboard.h index 2b55396..19c2a3d 100644 --- a/blackboard/blackboard.h +++ b/blackboard/blackboard.h @@ -56,6 +56,8 @@ public: void assign_var(const StringName &p_name, const BBVariable &p_var); + void link_var(const StringName &p_name, const Ref &p_target_blackboard, const StringName &p_target_var); + // TODO: Add serialization API. }; diff --git a/doc/source/classes/class_blackboard.rst b/doc/source/classes/class_blackboard.rst index ebd9a35..8cbc210 100644 --- a/doc/source/classes/class_blackboard.rst +++ b/doc/source/classes/class_blackboard.rst @@ -33,25 +33,27 @@ Methods .. table:: :widths: auto - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | void | :ref:`bind_var_to_property` **(** StringName var_name, Object object, StringName property **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | void | :ref:`erase_var` **(** StringName var_name **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | :ref:`Blackboard` | :ref:`get_parent` **(** **)** |const| | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | Variant | :ref:`get_var` **(** StringName var_name, Variant default=null, bool complain=true **)** |const| | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | bool | :ref:`has_var` **(** StringName var_name **)** |const| | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | void | :ref:`set_parent` **(** :ref:`Blackboard` blackboard **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | void | :ref:`set_var` **(** StringName var_name, Variant value **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | :ref:`Blackboard` | :ref:`top` **(** **)** |const| | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ - | void | :ref:`unbind_var` **(** StringName var_name **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`bind_var_to_property` **(** StringName var_name, Object object, StringName property **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`erase_var` **(** StringName var_name **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | :ref:`Blackboard` | :ref:`get_parent` **(** **)** |const| | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | Variant | :ref:`get_var` **(** StringName var_name, Variant default=null, bool complain=true **)** |const| | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | bool | :ref:`has_var` **(** StringName var_name **)** |const| | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`link_var` **(** StringName var_name, :ref:`Blackboard` target_blackboard, StringName target_var **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`set_parent` **(** :ref:`Blackboard` blackboard **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`set_var` **(** StringName var_name, Variant value **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | :ref:`Blackboard` | :ref:`top` **(** **)** |const| | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`unbind_var` **(** StringName var_name **)** | + +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator @@ -122,6 +124,22 @@ Returns ``true`` if the Blackboard contains the ``var_name`` variable, including ---- +.. _class_Blackboard_method_link_var: + +.. rst-class:: classref-method + +void **link_var** **(** StringName var_name, :ref:`Blackboard` target_blackboard, StringName target_var **)** + +Links a variable to another Blackboard variable. If a variable is linked to another variable, their state will always be identical, and any change to one will be reflected in the other. You can use this method to link a variable in the current scope to a variable in another scope, or in another Blackboard instance. + + + +A variable can only be linked to one other variable. Calling this method again will overwrite the previous link. However, it is possible to link to the same variable from multiple different variables. + +.. rst-class:: classref-item-separator + +---- + .. _class_Blackboard_method_set_parent: .. rst-class:: classref-method diff --git a/doc_classes/Blackboard.xml b/doc_classes/Blackboard.xml index 41ee2a9..b96a742 100644 --- a/doc_classes/Blackboard.xml +++ b/doc_classes/Blackboard.xml @@ -49,6 +49,17 @@ Returns [code]true[/code] if the Blackboard contains the [param var_name] variable, including the parent scopes. + + + + + + + Links a variable to another Blackboard variable. If a variable is linked to another variable, their state will always be identical, and any change to one will be reflected in the other. You can use this method to link a variable in the current scope to a variable in another scope, or in another Blackboard instance. + + A variable can only be linked to one other variable. Calling this method again will overwrite the previous link. However, it is possible to link to the same variable from multiple different variables. + +