Honestly, changing the parent was unncessary

My code differs in the fact that my enemies become the child of the randomly chosen spawn point where they, well, spawn.

It didn’t feel “correct”, let’s say, to change that logic to have all enemies placed under the Game scene. Specially because this neither made the actual signal connection any easier or harder.

What I did, is to keep my exact same logic, pass the enemy instance as a variable on the signal, and then called connect from the Game scene. I achieved the exact same result, since the parent scene itself was not the issue.

enemy_spawner.gd

func _spawn_enemy() -> void:
	var enemy_instance: Enemy = packed_enemy.instantiate()
	var random_spawn_position = spawn_positions.pick_random()
	random_spawn_position.add_child(enemy_instance)

# I just send the signal, keeping all the logic exactly the same
	emit_signal("enemy_spawned", enemy_instance)

game.gd

# one way or another, the game scene can always get the enemy instance with no issues
func _on_enemy_spawner_enemy_spawned(enemy_instance: Enemy):
	enemy_instance.connect("died", func(): score += enemy_instance.score_value)
3 Likes

Good shout. add_child(enemy_instance) can still be done in the Enemy Spawner and emitting the signal “enemy_spawned” with the enemy_instance still allows the game node to connect to the enemy_instance “died” signal.

It seems better to have all the enemies as children of the Enemy Spawner. But not sure if this will break future code in the course.

Privacy & Terms