Wouldn't it be easier to add enemies as children of spawners?

I did it as something like this:

var enemy_scene = preload("res://scenes/enemy.tscn")
@onready var spawn_positions = $SpawnPositions
@onready var timer = $Timer

@export var min_delay_between_enemies = 0.5
@export var max_delay_between_enemies = 3

var rng = RandomNumberGenerator.new()

func choose_random_spawner():
	return spawn_positions.get_children().pick_random()


func spawn_enemy():
	var enemy_instance = enemy_scene.instantiate()
	choose_random_spawner().add_child(enemy_instance)


func run_random_delay_timer():
	var timer_delay = rng.randf_range(min_delay_between_enemies, max_delay_between_enemies)
	timer.start(timer_delay)


func _on_timer_timeout():
	spawn_enemy()
	run_random_delay_timer()

So, instead of setting the global position of enemies, I simply set them as children of the respective spawners.

Also, I made the timer “one shot” and made the delay random, although I am not completely sure if it is the best approach.

2 Likes

Although, on the other hand, if I ever was to move in the code any of the individual spawners, that would move all rockets that were shot from it. So, I guess it work in this particular scenario but in general it might be not the best practice in other scenarios.

1 Like

I think you are right its a case by case option and for this course we kept it simple but its fine to add this level of complexity if your game needs it and it can be neater without breaking anything in the future.

Nice job thinking outside the course constraints :slight_smile:

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms