The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+10 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 (262 points)

1 Answer

+27 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 (262 points)

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

BTW looks like it was added in the 4.0 docs.

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 Frequently asked questions and 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 [email protected] with your username.