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.