Blackboard: Linking vars with `Blackboard::link_var`

This commit is contained in:
Serhii Snitsaruk 2024-03-12 16:17:34 +01:00
parent a6e6b53e18
commit 434545ebad
4 changed files with 58 additions and 19 deletions

View File

@ -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<Blackboard> &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);
}

View File

@ -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<Blackboard> &p_target_blackboard, const StringName &p_target_var);
// TODO: Add serialization API.
};

View File

@ -33,25 +33,27 @@ Methods
.. table::
:widths: auto
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`bind_var_to_property<class_Blackboard_method_bind_var_to_property>` **(** StringName var_name, Object object, StringName property **)** |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`erase_var<class_Blackboard_method_erase_var>` **(** StringName var_name **)** |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Blackboard<class_Blackboard>` | :ref:`get_parent<class_Blackboard_method_get_parent>` **(** **)** |const| |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variant | :ref:`get_var<class_Blackboard_method_get_var>` **(** StringName var_name, Variant default=null, bool complain=true **)** |const| |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bool | :ref:`has_var<class_Blackboard_method_has_var>` **(** StringName var_name **)** |const| |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`link_var<class_Blackboard_method_link_var>` **(** StringName var_name, :ref:`Blackboard<class_Blackboard>` target_blackboard, StringName target_var **)** |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`set_parent<class_Blackboard_method_set_parent>` **(** :ref:`Blackboard<class_Blackboard>` blackboard **)** |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`set_var<class_Blackboard_method_set_var>` **(** StringName var_name, Variant value **)** |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Blackboard<class_Blackboard>` | :ref:`top<class_Blackboard_method_top>` **(** **)** |const| |
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
+-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| void | :ref:`unbind_var<class_Blackboard_method_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<class_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

View File

@ -49,6 +49,17 @@
Returns [code]true[/code] if the Blackboard contains the [param var_name] variable, including the parent scopes.
</description>
</method>
<method name="link_var">
<return type="void" />
<param index="0" name="var_name" type="StringName" />
<param index="1" name="target_blackboard" type="Blackboard" />
<param index="2" name="target_var" type="StringName" />
<description>
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.
</description>
</method>
<method name="set_parent">
<return type="void" />
<param index="0" name="blackboard" type="Blackboard" />