+7 votes



can return a null object, which is not "Null", but it's an empty object, so you cannot do:

if object == null:

I'm currently doing:

if str(scene_root) == "[Object:null]":

which is obviously a hack.

How can you handle null objects the correct way?

in Engine by (37 points)

2 Answers

+9 votes
Best answer

Godot 3.1 will have an is_instance_valid() function that can tell if Object was freed.
It's used like this:

is_instance_valid( object )

It's available in Godot 3.1 alpha.

Before 3.1 we can use weakref to hold objects. If object was freed weakref compares equal to null.

You will need a standard reference alongside weakref and change them together.
Not an ideal solution, i know, but at least it does not depend on a comparison to a string that can vary, for example i'm getting "Deleted Object", not "[Object:null]".

by (2,274 points)
selected by
0 votes

Well another way would be to do this unless you named an object to _NULL.

if btn == $_NULL:
by (719 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.