+7 votes

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........

func _on_Player_area_entered(area): 
       enemies.append(area)   # append Area2D
in Engine by (44 points)
edited by

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 queuefree() the object should give true for isqueuedfordeletion() 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.

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.
queuefree() is executed using areaentered signal.

func _on_PlayerBullet_area_entered(area):

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://godotengine.org/qa/2773/detect-if-an-object-reference-is-freed

1 Answer

+10 votes
Best answer

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

see more details from here


by (9,768 points)
selected by

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

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 Frequently asked questions and 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 [email protected] with your username.