How do I free instanced scenes if they aren't placed into the main scene from the editor?

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

Hi all. So very close to getting a game functioning the way I envisioned. Little problem, though…

I spawn in my enemies at one side of the screen, and they move to the opposite side. That part works. The first enemy that reaches the Player’s base is freed from the queue, and stops the enemies from spawning. That also works. But how do I apply queue_free() to the remaining enemies?

I think my Level.gd script would be necessary. This section is from the part that initiates “Game Over”

func _on_BaseArea_area_entered(area):
    print("Collision!")
    $Node/SpawnTimer.stop()
    $Node/SurvivalTimer.stop()
    $HUD.player_loss() 
    emit_signal('game_over')
    $BaseArea/CollisionShape2D.disabled = true
    get_tree().call_group("EyeWorms", "queue_free") # This is the new
# line.

System_Error | 2018-12-17 17:12

:bust_in_silhouette: Reply From: SIsilicon

You can add the enemy scene itself to a user-defined group. Call the group anything you want; like enemies. Any instances of the scene will be included in this group. Then all you need to do to free them all at once is-

get_tree.call_group("enemies", "queue_free")

After a little poking around, I put

get_tree().call_group("EyeWorms", "queue_free")

into the code block. Doesn’t do anything as far as I can see.

Did I not do something right?

System_Error | 2018-12-17 18:33

Did you add the enemy scene to a group called EyeWorms? Just so you know, the naming is case-sensitive.

SIsilicon | 2018-12-17 21:21

Ah, silly me. I wrote “enemies” instead of “EyeWorms”. The enemies were in the group, I just forgot to actually call said group. Thanks!

System_Error | 2018-12-17 23:36