How can I detect [Deleted Object] in the array?

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

When Area2D in the array was destroyed, [Deleted Object] was displayed.
I want to detect it with code.
I compare it with null in line 5, but it did not work.

How can I detect [Deleted Object] in the array?

var enemies = []
func _process(delta):
       print(enemies)  # [[Area2D:1445], [Deleted Object], [Area2D:1453]]
       for i in enemies.size():
               if enemies[i] == null: # not working........
                     print("empty")

func _on_Player_area_entered(area):	
       enemies.append(area)   # append Area2D

How is this Area deleted? Normally it shouldn’t be deleted unless all references (including reference in the array) are gone.

So if an Object is deleted with queue_free() the object should give true for is_queued_for_deletion() until the array entry is overwritten or deleted.

Anyway, you could try testing with enemies[i] is null because null is supposed to be a special type in GDScript.

Or: Delete the array entry in enemies at the moment you’re deleting the Area. (All indices will change)

Or: Use a hash instead of an array. That way you can directly remove the hash entry on deleting the area. You would have to keep a counter though.

wombatstampede | 2019-02-10 11:56

Thank you for your reply, wombatstampede !

Player (Area2D) has an enemies array. (above code)
There is PlayerBullet (Area2D) which is a class different from Player.

When PlayerBullet hits the enemy, it automatically deletes the enemy.
queue_free() is executed using area_entered signal.

func _on_PlayerBullet_area_entered(area):
    area.queue_free()

I want to detect and delete those destroyed in the enemies array.

It was not detected by null.
So, I am trying this now. → https://forum.godotengine.org/2773/detect-if-an-object-reference-is-freed

Eclair | 2019-02-10 12:59

:bust_in_silhouette: Reply From: volzhs

you can use is_instance_valid() if you are using 3.1
or weakref() if 3.0.x

see more details from here

https://forum.godotengine.org/33196/how-are-you-supposed-to-handle-null-objects?show=33205#a33205

is_instance_valid()

The answer I was looking for is exactly this!
Thank you so much, volzhs! XD

Eclair | 2019-02-10 13:25