Godot-proposals: Add 'has_property()' and 'has_constant()' to Object class

Created on 16 Apr 2020  路  5Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:
Godot tutorial (N/A)

Describe the problem or limitation you are having in your project:
The Object class has no function to check for the existence of constants or properties (variables) in an object.
has_method() already exists, so has_property() and has_constant() should be added.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
As mentioned by RabbitB in the comments, the Script class already has functions for this.
Therefore, the new functions added to the Object class could simply make calls to the already implemented functions in the Script class.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

func has_property(name: String) -> bool:
    var my_script: Script = get_script() as Script
    return my_script.get_script_property_list().has(name) if my_script else false

func has_constant(name: String) -> bool:
    var my_script: Script = get_script() as Script
    return my_script.get_script_constant_map().keys().has(name) if my_script else false

If this enhancement will not be used often, can it be worked around with a few lines of script?:
I think it would be used often, but you _could_ put these in a singleton with an additional object parameter.

Is there a reason why this should be core and not an add-on in the asset library?:
These would be a very useful to have built-in, for the same reason that has_method() is useful.
The user would expect something like has_property() since has_method() already exists.

core

Most helpful comment

These would be a very useful to have built-in, for the same reason that has_method() is useful.
The user would expect something like has_property() since has_method() already exists.

馃憤

When I first tried to check whether a particular object has a property (and given the already existing knowledge about methods such as has_method or has_signal), I was kind of disappointed about the fact that there's no analogous has_property. The @bojidar-bg's suggestion seems to solve the use case, but we're talking about consistency here. The in semantics has already (too many) ways for the code to become even more confusing.

All 5 comments

This is already doable, it's just hidden away in the Script class instead.

func has_property(name: String) -> bool:
    var my_script: Script = get_script() as Script
    return my_script.get_script_property_list().has(name) if my_script else false

func has_constant(name: String) -> bool:
    var my_script: Script = get_script() as Script
    return my_script.get_script_constant_map().keys().has(name) if my_script else false

This is already doable, it's just hidden away in the Script class instead.

Wow, I don't know how I missed that!
I'll go ahead and close this then.
Thanks!

I would like to add that for x.has_property(y), you can also do y in x in GDScript.

These would be a very useful to have built-in, for the same reason that has_method() is useful.
The user would expect something like has_property() since has_method() already exists.

馃憤

When I first tried to check whether a particular object has a property (and given the already existing knowledge about methods such as has_method or has_signal), I was kind of disappointed about the fact that there's no analogous has_property. The @bojidar-bg's suggestion seems to solve the use case, but we're talking about consistency here. The in semantics has already (too many) ways for the code to become even more confusing.

Reopened with updated post.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WilliamTambellini picture WilliamTambellini  路  3Comments

regakakobigman picture regakakobigman  路  3Comments

Xrayez picture Xrayez  路  3Comments

SpyrexDE picture SpyrexDE  路  3Comments

arkology picture arkology  路  3Comments