From c739a876b0f767e4530b0091858135aeafd26c94 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 9 Apr 2024 11:34:22 +0200 Subject: [PATCH] Blackboard: Add `create` parameter to `bind_var_to_property` and `link_var` --- blackboard/blackboard.cpp | 26 ++++++++---- blackboard/blackboard.h | 4 +- doc/source/classes/class_blackboard.rst | 54 ++++++++++++------------- doc_classes/Blackboard.xml | 9 +++-- 4 files changed, 52 insertions(+), 41 deletions(-) diff --git a/blackboard/blackboard.cpp b/blackboard/blackboard.cpp index af55d89..4819013 100644 --- a/blackboard/blackboard.cpp +++ b/blackboard/blackboard.cpp @@ -64,13 +64,19 @@ void Blackboard::erase_var(const StringName &p_name) { data.erase(p_name); } -void Blackboard::bind_var_to_property(const StringName &p_name, Object *p_object, const StringName &p_property) { - ERR_FAIL_COND_MSG(!data.has(p_name), "Blackboard: Binding failed - can't bind variable that doesn't exist."); +void Blackboard::bind_var_to_property(const StringName &p_name, Object *p_object, const StringName &p_property, bool p_create) { + if (!data.has(p_name)) { + if (p_create) { + data.insert(p_name, BBVariable()); + } else { + ERR_FAIL_MSG("Blackboard: Can't bind variable that doesn't exist (var: " + p_name + ")."); + } + } data[p_name].bind(p_object, p_property); } void Blackboard::unbind_var(const StringName &p_name) { - ERR_FAIL_COND_MSG(data.has(p_name), "Blackboard: Can't unbind variable that doesn't exist."); + ERR_FAIL_COND_MSG(data.has(p_name), "Blackboard: Can't unbind variable that doesn't exist (var: " + p_name + ")."); data[p_name].unbind(); } @@ -78,8 +84,14 @@ 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 + ")."); +void Blackboard::link_var(const StringName &p_name, const Ref &p_target_blackboard, const StringName &p_target_var, bool p_create) { + if (!data.has(p_name)) { + if (p_create) { + data.insert(p_name, BBVariable()); + } else { + ERR_FAIL_MSG("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]; @@ -93,7 +105,7 @@ void Blackboard::_bind_methods() { ClassDB::bind_method(D_METHOD("get_parent"), &Blackboard::get_parent); ClassDB::bind_method(D_METHOD("erase_var", "var_name"), &Blackboard::erase_var); 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("bind_var_to_property", "var_name", "object", "property", "create"), &Blackboard::bind_var_to_property, DEFVAL(false)); 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); + ClassDB::bind_method(D_METHOD("link_var", "var_name", "target_blackboard", "target_var", "create"), &Blackboard::link_var, DEFVAL(false)); } diff --git a/blackboard/blackboard.h b/blackboard/blackboard.h index 19c2a3d..83d8ee8 100644 --- a/blackboard/blackboard.h +++ b/blackboard/blackboard.h @@ -51,12 +51,12 @@ public: bool has_var(const StringName &p_name) const; void erase_var(const StringName &p_name); - void bind_var_to_property(const StringName &p_name, Object *p_object, const StringName &p_property); + void bind_var_to_property(const StringName &p_name, Object *p_object, const StringName &p_property, bool p_create = false); void unbind_var(const StringName &p_name); 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); + void link_var(const StringName &p_name, const Ref &p_target_blackboard, const StringName &p_target_var, bool p_create = false); // TODO: Add serialization API. }; diff --git a/doc/source/classes/class_blackboard.rst b/doc/source/classes/class_blackboard.rst index 8cbc210..8661ca4 100644 --- a/doc/source/classes/class_blackboard.rst +++ b/doc/source/classes/class_blackboard.rst @@ -33,27 +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:`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 **)** | - +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ + +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | void | :ref:`bind_var_to_property` **(** StringName var_name, Object object, StringName property, bool create **)** | + +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | 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, bool create **)** | + +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | 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 @@ -68,9 +68,9 @@ Method Descriptions .. rst-class:: classref-method -void **bind_var_to_property** **(** StringName var_name, Object object, StringName property **)** +void **bind_var_to_property** **(** StringName var_name, Object object, StringName property, bool create **)** -Establish a binding between a variable and the object's property specified by ``property`` and ``object``. Changes to the variable update the property, and vice versa. +Establish a binding between a variable and the object's property specified by ``property`` and ``object``. Changes to the variable update the property, and vice versa. If ``create`` is ``true``, the variable will be created if it doesn't exist. .. rst-class:: classref-item-separator @@ -128,13 +128,11 @@ Returns ``true`` if the Blackboard contains the ``var_name`` variable, including .. rst-class:: classref-method -void **link_var** **(** StringName var_name, :ref:`Blackboard` target_blackboard, StringName target_var **)** +void **link_var** **(** StringName var_name, :ref:`Blackboard` target_blackboard, StringName target_var, bool create **)** -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. +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. If ``create`` is ``true``, the variable will be created if it doesn't exist. - - -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. +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 diff --git a/doc_classes/Blackboard.xml b/doc_classes/Blackboard.xml index b96a742..b50e562 100644 --- a/doc_classes/Blackboard.xml +++ b/doc_classes/Blackboard.xml @@ -16,8 +16,9 @@ + - Establish a binding between a variable and the object's property specified by [param property] and [param object]. Changes to the variable update the property, and vice versa. + Establish a binding between a variable and the object's property specified by [param property] and [param object]. Changes to the variable update the property, and vice versa. If [param create] is [code]true[/code], the variable will be created if it doesn't exist. @@ -54,10 +55,10 @@ + - 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. + 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. If [param create] is [code]true[/code], the variable will be created if it doesn't exist. + 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.