-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Comments
This comment has been minimized.
This comment has been minimized.
get_meta()
to return a default value (similar to Dictionary.get()
)
For 4.0, I think we should modify all the I don't think we can rely on the value of the proposed second parameter in |
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. |
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 addhas_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 likeDictionary.get()
). Alternatively, if erroring non-existent metas is desired, newget_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
or
While with my proposal it could just be
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.
The text was updated successfully, but these errors were encountered: