diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-03-29 16:14:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 16:14:28 +0200 |
commit | 482cdeaf718d77c24783a6b73281bfed886474ed (patch) | |
tree | 5f62ccdc70584239a7bbb5384de76cbe5f1cdcde /core/object | |
parent | ab24fb226da64b99938ffdc43e782c5140ee3f7c (diff) | |
parent | 3eb7fc4bfd376bc27ea3313ccd1da4679c48f4cb (diff) |
Merge pull request #58608 from KoBeWi/metadefault
Diffstat (limited to 'core/object')
-rw-r--r-- | core/object/object.cpp | 12 | ||||
-rw-r--r-- | core/object/object.h | 2 |
2 files changed, 10 insertions, 4 deletions
diff --git a/core/object/object.cpp b/core/object/object.cpp index 7db62fdc5c..0a7879c774 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -956,8 +956,14 @@ void Object::set_meta(const StringName &p_name, const Variant &p_value) { } } -Variant Object::get_meta(const StringName &p_name) const { - ERR_FAIL_COND_V_MSG(!metadata.has(p_name), Variant(), "The object does not have any 'meta' values with the key '" + p_name + "'."); +Variant Object::get_meta(const StringName &p_name, const Variant &p_default) const { + if (!metadata.has(p_name)) { + if (p_default != Variant()) { + return p_default; + } else { + ERR_FAIL_V_MSG(Variant(), "The object does not have any 'meta' values with the key '" + p_name + "'."); + } + } return metadata[p_name]; } @@ -1559,7 +1565,7 @@ void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta); ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta); - ClassDB::bind_method(D_METHOD("get_meta", "name"), &Object::get_meta); + ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta); ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind); diff --git a/core/object/object.h b/core/object/object.h index dbaf9f13fd..f58a79b49c 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -745,7 +745,7 @@ public: bool has_meta(const StringName &p_name) const; void set_meta(const StringName &p_name, const Variant &p_value); void remove_meta(const StringName &p_name); - Variant get_meta(const StringName &p_name) const; + Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const; void get_meta_list(List<StringName> *p_list) const; #ifdef TOOLS_ENABLED |