From 6d36d20d06da88e97340d638be900aa7b8073107 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 13 Feb 2024 17:07:53 +0100 Subject: [PATCH] Blackboard: Complain when variable is missing --- blackboard/blackboard.cpp | 7 +++++-- blackboard/blackboard.h | 2 +- doc_classes/Blackboard.xml | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/blackboard/blackboard.cpp b/blackboard/blackboard.cpp index 6e3f8c8..00075bf 100644 --- a/blackboard/blackboard.cpp +++ b/blackboard/blackboard.cpp @@ -32,12 +32,15 @@ Ref Blackboard::top() const { return bb; } -Variant Blackboard::get_var(const String &p_name, const Variant &p_default) const { +Variant Blackboard::get_var(const String &p_name, const Variant &p_default, bool p_complain) const { if (data.has(p_name)) { return data.get(p_name).get_value(); } else if (parent.is_valid()) { return parent->get_var(p_name, p_default); } else { + if (p_complain) { + ERR_PRINT(vformat("Blackboard: Variable \"%s\" not found.", p_name)); + } return p_default; } } @@ -90,7 +93,7 @@ void Blackboard::prefetch_nodepath_vars(Node *p_node) { } void Blackboard::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default"), &Blackboard::get_var, Variant()); + ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default", "p_complain"), &Blackboard::get_var, Variant(), true); ClassDB::bind_method(D_METHOD("set_var", "p_name", "p_value"), &Blackboard::set_var); ClassDB::bind_method(D_METHOD("has_var", "p_name"), &Blackboard::has_var); ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent); diff --git a/blackboard/blackboard.h b/blackboard/blackboard.h index eba492b..ee0987e 100644 --- a/blackboard/blackboard.h +++ b/blackboard/blackboard.h @@ -46,7 +46,7 @@ public: Ref top() const; - Variant get_var(const String &p_name, const Variant &p_default) const; + Variant get_var(const String &p_name, const Variant &p_default, bool p_complain = true) const; void set_var(const String &p_name, const Variant &p_value); bool has_var(const String &p_name) const; void erase_var(const String &p_name); diff --git a/doc_classes/Blackboard.xml b/doc_classes/Blackboard.xml index 5d429e0..126e656 100644 --- a/doc_classes/Blackboard.xml +++ b/doc_classes/Blackboard.xml @@ -37,8 +37,9 @@ + - Returns variable value. + Returns variable value or [param p_default] if variable doesn't exist. If [param p_complain] is [code]true[/code], an error will be printed if variable doesn't exist.