In GDScript how to (quickly) check whether an object instance has a property

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chanon

Is there a way to quickly and easily check an object instance whether it has a property?

For example in Object API there is a has_method method that can check if the object has a named method. Is there a has_property method?

There is a get_property_list method, but I just want to quickly check existance of a property, not necessarily prepare a whole Array of Dictionaries of properties.

:bust_in_silhouette: Reply From: chanon

I found the answer. Use the in keyword.

For example to check if object1 has the text property you can do

if "text" in object1:
    # do something

The “in” keyword is not documented, I think it should be include in the documentation.

TGMG | 2021-08-26 18:49

BTW looks like it was added in the 4.0 docs.

nanodeath | 2023-06-23 02:52

1 Like