diff options
author | kobewi <kobewi4e@gmail.com> | 2022-02-27 22:03:33 +0100 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2022-03-22 15:50:47 +0100 |
commit | 3eb7fc4bfd376bc27ea3313ccd1da4679c48f4cb (patch) | |
tree | 2dfe483a2a36aca51131c309f378d74684822dbe | |
parent | 9433c1d70c67fe8294d2c5721cf7e4b6cb3d554b (diff) |
Add optional 'default' argument to get_meta()
-rw-r--r-- | core/object/object.cpp | 12 | ||||
-rw-r--r-- | core/object/object.h | 2 | ||||
-rw-r--r-- | doc/classes/Object.xml | 2 |
3 files changed, 12 insertions, 4 deletions
diff --git a/core/object/object.cpp b/core/object/object.cpp index 096edd4e60..60b0fcc104 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -922,8 +922,14 @@ void Object::set_meta(const StringName &p_name, const Variant &p_value) { metadata[p_name] = 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]; } @@ -1529,7 +1535,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 6b4f1c81e6..9ccad8b513 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -761,7 +761,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 diff --git a/doc/classes/Object.xml b/doc/classes/Object.xml index d09f3a2b0d..77927b9a8a 100644 --- a/doc/classes/Object.xml +++ b/doc/classes/Object.xml @@ -366,8 +366,10 @@ <method name="get_meta" qualifiers="const"> <return type="Variant" /> <argument index="0" name="name" type="StringName" /> + <argument index="1" name="default" type="Variant" default="null" /> <description> Returns the object's metadata entry for the given [code]name[/code]. + Throws error if the entry does not exist, unless [code]default[/code] is not [code]null[/code] (in which case the default value will be returned). </description> </method> <method name="get_meta_list" qualifiers="const"> |