Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional default value to get_meta() #58608

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ class Object {
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
Expand Down
2 changes: 2 additions & 0 deletions doc/classes/Object.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down