Removing objects when a specific timer timeout

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

Hello Again Community :slight_smile:
.
i’ve made space invaders game
i connected Aliens ((RigidBody2D)) with a timer in main node

when the player Die ((Area2d))
alien Timer stops and new aliens stop coming that’s Good Until now

But Aliens Which are already in the game screen stays
and when i press new game i see the old aliens still on the screen
.
how i can remove Them !!!
(( aliens Node are instance child in the main node))
.
i tried a lot of functions to delete them but i’ve failed :frowning:

:bust_in_silhouette: Reply From: deaton64

Hi,
Add all the aliens to a group. When the player dies, send a signal to the group. When the alien gets the signal, make it die.

So something like:

in the alien script _ready() function:

add_to_group("alien")

Add a function for the alien to react to the death of the player:

func player_dead() -> void:
      # make the alien gloat ;)
      queue_free()

Then in your player script, when the player dies:

get_tree().call_group("alien", "player_dead")	

Thank you so much… dude :slight_smile:

Zylo_X | 2020-04-21 20:27

:bust_in_silhouette: Reply From: supper_raptor

Add aliens in group enemy and when the player dies run this code

var enemies = get_tree().get_nodes_in_group("enemy")
for i in enemies:
	i.queue_free()

Thank you So much I Will try it

Zylo_X | 2020-04-21 20:26