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

Allow get_meta() to return a default value (similar to Dictionary.get()) #2383

Closed
KoBeWi opened this issue Mar 1, 2021 · 3 comments · Fixed by godotengine/godot#58608
Closed
Milestone

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Mar 1, 2021

Describe the project you are working on

Game using GDScript

Describe the problem or limitation you are having in your project

I use object metadata quite a lot. My problem is that get_meta() throws an error when given meta doesn't exist. This is often inconvenient, because I have to add has_meta() in a way that makes the code more ugly.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

get_meta() could take second argument, in which case instead of error that value would be returned (just like Dictionary.get()). Alternatively, if erroring non-existent metas is desired, new get_meta_or_default() method could be added.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Let's say I want to add a meta property when a projectile hits an object. It will be used by this specific projectile, so there's no reason to add a regular property. Also this meta will be increased with each hit.

Currently the code has to be either

var hit_bonus = 0
if object.has_meta("hit_bonus"):
  hit_bonus = object.get_meta("hit_bonus") + 1
object.set_meta("hit_bonus", hit_bonus)

or

if object.has_meta("hit_bonus"):
  object.set_meta("hit_bonus", object.get_meta("hit_bonus") + 1)
else:
  object.set_meta("hit_bonus", 1)

While with my proposal it could just be

var hit_bonus = object.get_meta("hit_bonus", 0)
object.set_meta("hit_bonus", hit_bonus + 1)

It's twice shorter.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Well, it can, but I'd use it relatively often.

Is there a reason why this should be core and not an add-on in the asset library?

You can't extend existing types and alternative would be some utility singleton.

@AaronRecord

This comment has been minimized.

@Calinou Calinou changed the title Allow get_meta() return default value similar to Dictionary.get() Allow get_meta() to return a default value (similar to Dictionary.get()) Mar 1, 2021
@Calinou
Copy link
Member

Calinou commented Mar 1, 2021

For 4.0, I think we should modify all the get() methods (Object.get(), Object.get_meta(), Dictionary.get()) to consistently print error messages when the value can't be found. To silence the error, you'll have to use new methods like Object.get_or_null(), Object.get_meta_or_null().
We already have Node.get_node_or_null() as an example of this.

I don't think we can rely on the value of the proposed second parameter in Object.get_meta() to silence the error as the default value of the second parameter will be null (since it can be any Variant).

@Bromeon
Copy link

Bromeon commented Mar 1, 2021

I agree with @Calinou, that approach combined with nullable types could greatly enhance type safety:

func get_node(path: NodePath) -> Node            # always valid, client code doesn't need to check
func get_node_or_null(path: NodePath) -> Node?   # can be null, not checking may cause warning

That's also how Kotlin does it.
However, this only works if errors are handled in a way other than simply return null + print, otherwise the type safety is broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants