Delete spawner after the enemy is spawned without destroying the enemy

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

Hello! I’m trying to build a first game in Godot after a few years in GameMaker. So far I’m not sure about the node structure even though I’ve checked a number of tutorials. So far I have a Game node where on timer_timeout I spawn a spawner of my enemy. The reason I have a spawner of a spawner is because I first create node with animation of spawn and then on animation_finished I create the enemy (Jack) itself.

Here is relevant code:
Node Game

func _on_SpawnTimer_timeout():
var cellSize = 24
var xPos = stepify(rand_range(0, 240),cellSize) + cellSize/2
var yPos = stepify(rand_range(0, 426),cellSize) + cellSize/2
var spawn = SpawnerJack.instance()
spawn.position = Vector2(xPos, yPos)

Node SpawnerJack

func _on_AnimatedSprite_animation_finished():
var spawn = Jack.instance()

queue_free() in SpawnerJack won’t work as it will destroy the enemy as well so I’m not sure what else I can do… I would really appreciate a nudge in the correct direction.

:bust_in_silhouette: Reply From: kidscancode

If you make the enemy a child of the spawner, then freeing the spawner will also free the enemy.

Solution: parent your enemy to another node, such as “Game”.

Thank you for chiming in. How would I do that? I’m spawning the enemy (Jack) on animation_finished of the SpawnerJack. Can I call that function from another node (Game)? How?

SpaceyProt | 2020-02-24 16:11

You can call add_child() on any node. For example, assuming “Game” is the root node of your game:


And the instance will be the child of “Game”. Note that if your node path is different, you’ll need to adjust that.

kidscancode | 2020-02-24 16:36

Great! I only had to specify the position again. Thank you!

SpaceyProt | 2020-02-24 19:46