+6 votes

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.

in Engine by (252 points)

1 Answer

+13 votes
Best answer

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
by (252 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.