+5 votes

I have an a reference of an object and I want to detect if the object has been freed with queue_free.
This is the output after the object has been freed
print("obj:" + str(obj))

before queue free
obj: [RigidBody2D:1000]
after queue free
obj: [Deleted Object]

I want to test something like


obj == null don't work on this case.

So How to do this on Godot?

in Engine by (76 points)
edited by

4 Answers

+12 votes

While you know the object is stll alive:

var wr = weakref(object);

Then you can check like this:

if (!wr.get_ref()):
     #object is erased
    #object is fine so you can do something with it:
by (1,293 points)

This is exactly what @hearto and I were looking for. Thanks you!

Can someone tell me why a while loop causes execution to freeze? E.g.

while ! wr.get_ref():
    # object is erased

You saved my life, thanks a lot!!!

+6 votes
by (81 points)

Thank you! Awesome. Much easier than the weakref

0 votes

i put every item in groups.
you can check the group how many members it has or if an specific objekt is inside it.

if it has been freed the group dosnt contain it anymore.

by (68 points)
0 votes

Use the signal "tree_exited" to know when something was freed. This is indirect. It will work on items that are in the scene tree.
Warning: if an item is never added to the scene tree, this will not work.

by (24 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.