0 votes

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 timertimeout 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 animationfinished 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()
add_child(spawn)
spawn.position = Vector2(xPos, yPos)

Node SpawnerJack

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

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.

in Engine by (15 points)

1 Answer

+2 votes
Best answer

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

by (20,479 points)
selected by

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?

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

get_node("/root/Game").add_child(spawn)

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

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

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 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 webmaster@godotengine.org with your username.