Add optional 'default' argument to get_meta()

(cherry picked from commit 3eb7fc4bfd)
This commit is contained in:
kobewi 2022-02-27 22:03:33 +01:00 committed by Rémi Verschelde
parent f28309aa86
commit e86a64a2a5
3 changed files with 12 additions and 5 deletions

View file

@ -1061,8 +1061,15 @@ void Object::set_meta(const String &p_name, const Variant &p_value) {
metadata[p_name] = p_value; metadata[p_name] = p_value;
} }
Variant Object::get_meta(const String &p_name) const { Variant Object::get_meta(const String &p_name, const Variant &p_default) const {
ERR_FAIL_COND_V(!metadata.has(p_name), Variant()); ERR_FAIL_COND_V(!metadata.has(p_name), Variant());
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]; return metadata[p_name];
} }
@ -1675,7 +1682,7 @@ void Object::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta); 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("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("has_meta", "name"), &Object::has_meta);
ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind); ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);

View file

@ -685,12 +685,10 @@ public:
void set_script(const RefPtr &p_script); void set_script(const RefPtr &p_script);
RefPtr get_script() const; RefPtr get_script() const;
/* SCRIPT */
bool has_meta(const String &p_name) const; bool has_meta(const String &p_name) const;
void set_meta(const String &p_name, const Variant &p_value); void set_meta(const String &p_name, const Variant &p_value);
void remove_meta(const String &p_name); void remove_meta(const String &p_name);
Variant get_meta(const String &p_name) const; Variant get_meta(const String &p_name, const Variant &p_default = Variant()) const;
void get_meta_list(List<String> *p_list) const; void get_meta_list(List<String> *p_list) const;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED

View file

@ -215,8 +215,10 @@
<method name="get_meta" qualifiers="const"> <method name="get_meta" qualifiers="const">
<return type="Variant" /> <return type="Variant" />
<argument index="0" name="name" type="String" /> <argument index="0" name="name" type="String" />
<argument index="1" name="default" type="Variant" default="null" />
<description> <description>
Returns the object's metadata entry for the given [code]name[/code]. 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> </description>
</method> </method>
<method name="get_meta_list" qualifiers="const"> <method name="get_meta_list" qualifiers="const">