system
April 8, 2016, 6:29am
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
hearto
Old Version
Published before Godot 3 was released.
Hello,
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
if(is_deleted(obj)):
do_something
obj == null don’t work on this case.
So How to do this on Godot?
system
April 8, 2016, 4:01pm
4
Reply From:
kubecz3k
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
else:
#object is fine so you can do something with it:
wr.get_ref().doSomeOperation();
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
stevepop | 2017-08-18 20:13
You saved my life, thanks a lot!!!
Chao Yu | 2017-11-17 15:41
system
August 23, 2019, 10:55am
5
Reply From:
Jay Kyburz
As of 3.1.1 you can call
is_instance_valid(node)
opened 01:44AM - 13 Apr 16 UTC
closed 10:19PM - 07 May 18 UTC
feature proposal
topic:gdscript
Please add a global function that lets us check if an instance has already been … freed.
I know, it's possible to avoid this situation by meticulously enforcing the order in which objects are removed from scene and object references are freed, but sometimes it would take tremendous effort to refactor the code to free the instance correctly / only once.
Currently, if an object was part of a display tree and its parent / grandparent has been freed, the object would be freed along with the parent, and a variable pointing to it would become invalid.
Attempting to call any function on this instance results in:
`Attempt to call function 'FUNCTION_NAME' in base 'previously freed instance' on a null instance.`
Since the Godot runtime is able to detect this and break with an error, I assume there's a way to check if an instance pointed at by a variable has been freed or not.
It would be a very very helpful addition and could save many hours of debugging. Something like:
`bool is_instance_valid( variable )`
Thankee sai.
committed 08:30PM - 07 May 18 UTC
Thank you! Awesome. Much easier than the weakref
NOAHGengo | 2020-05-17 20:24
system
August 23, 2019, 11:40am
6
Reply From:
gruen
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.
system
November 4, 2020, 1:51pm
7
Reply From:
JulSams
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.