distinguish between <Freed Object> and <null>

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

how do I distinguish between [Freed Object] and [null]

currently I am doing this but it’s hacky:

var object = <Freed Object> or <null>
if str(object) == "<Freed Object>":
    print("object is a Freed Object")
else:
    print("object is actually null")

is_instance_valid() does not work because it treats null and Freed Object as the same thing

:bust_in_silhouette: Reply From: SQBX

null and freed objects have different hashes

if hash(object) == hash(null):
    print("object is null")
elif object == null:
    print("object is freed object")

<Freed Object> == null returns true

OgGhostJelly | 2023-01-01 06:14

My bad, the above answer in incorrect, at least in Godot 4.x alpha. Is there a reason that said object has to be null, though? Could you instead make it start out as a string (I.e. ‘“undefined”’) and change it to and from an object that can be freed as needed and compare it to ‘“undefined”’ if needed?

SQBX | 2023-01-01 06:21

yeah that’s a good idea. I can just change the default value to “undefined” as a string, but isn’t the whole point of null to represent something that is undefined?

OgGhostJelly | 2023-01-02 03:13

Read the edited answer; TLDR I forgot hashes existed.

SQBX | 2023-01-02 03:52

tysm! sorry for the late answer

OgGhostJelly | 2023-01-07 13:57