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.
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.
Most helpful comment
馃憤
When I first tried to check whether a particular object has a property (and given the already existing knowledge about methods such as
has_methodorhas_signal), I was kind of disappointed about the fact that there's no analogoushas_property. The @bojidar-bg's suggestion seems to solve the use case, but we're talking about consistency here. Theinsemantics has already (too many) ways for the code to become even more confusing.