Topic was automatically imported from the old Question2Answer platform.
Asked By
Inces
Doesn’t need additional description
Everyone knows this error : unable… : previously freed instance on a null instance.
Is there a way for checking if a node in array ceased to exist, in a way not provoking this error ? Checking Inside_scene_tree and if = null won’t work.
You can also check if node.is_queued_for_deletion()
A really good example I encountered was when you have 2 Nodes A and B and both emit signals to each other and delete each other
Let’s say I have 2 Area2D nodes with the same script as such:
extends Area2D
func _area_entered(obj:Area2D): # area_entered signal connected
# do something here
obj.queue_free()
And they overlap as such:
You would expect that B would have more priority and hence would delete A
not giving a chance for A’s _area_entered() to be emitted and as such only A would be deleted? But nope that’s not how it works
In reality queue_free() waits for emitted signal functions to complete before deletion and as such both A & B get deleted (I learned this the hard way)
the solution? check for is_queued_for_deletion() !
extends Area2D
func _area_entered(obj:Area2D): # area_entered signal connected
if(self.is_queued_for_deletion()):
return
# do something here
obj.queue_free()
But Cakelover, you might ask, where would we actually use this?
Well this might particularly be helpful when merging the same instances like I tried