Get_children() doesn't return all children

I have a node (root node of the main scene) that spawns enemies. When it spawns enemies it should delete all of its children that are enemies. To do this I have the following code:

	# delete existing enemies
	print(get_children())
	for i in get_children():
		if i.editor_description == "Enemy":
			i.queue_free()

	# create new enemies
	for i in range(5):
		var enemy = enemies.pick_random().instantiate()
		call_deferred("add_child", enemy)

Creating new enemies works perfectly fine but get_children() returns every child except for the enemies. But when I look at the scene tree when running the game this is what I see:
image
But still, print(get_children()) only prints:
[Player:<CharacterBody2D#29561455848>, Energy:<Label#29645341943>]
How can I get a list of all these enemies so that I can delete them when I need to spawn new ones?

I would group all enemy scene nodes under a “mobs” or “enemies” group and iterate through that using the group methods.

Here’s a small example that could help you:

All you need then from your end is to programmatically add the procedural enemies to the group.

Nevermind, I realized I was stupid and I wasn’t actually running the code at all! Thank you, I think this would have been a solution if there even was a problem in the first place :sweat_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.