How are you supposed to handle null objects?

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



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?

:bust_in_silhouette: Reply From: hilfazer

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]”.

:bust_in_silhouette: Reply From: wyattb

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

if btn == $_NULL: