Randomize enemy spawn point

Godot Version

4.6.2

Question

I am trying to get the enemy to respawn in a random location every time they die, also if i could get it to spawn multiple enemies at the same time that would be great. the enemies are in the same scene as the map if this is needed. Sorry if this is obvious, i am very new to godot.

extends CharacterBody2D

@export var spawn_position = Vector2(-26, -79)
#@export var enemy_id: int
@export var respawn_time := 3.0

@onready var sprite: Sprite2D = $Sprite2D
@onready var collision_shape: CollisionShape2D = $CollisionShape2D
@onready var timer: Timer = $Timer


var invulnerable := false
var is_dead = false
var can_attack := true
var healing = false
var heal_cooldown = true
var health
var hit_cooldown = false
var original_layer: int
var original_mask: int
var speed = speedy

func _ready():
	original_layer = collision_layer
	original_mask = collision_mask
	door = get_tree().get_first_node_in_group("door")
	#sets spawn location
	if GameState.on_main_menu:
		GameState.enemy_2_positions = position
	if not is_dead:
		if not GameState.is_game_starting2:
			global_position = GameState.enemy_2_positions
		if GameState.is_game_starting2:
			global_position = spawn_position
			GameState.is_game_starting2 = false
	move_and_slide()

func _deferred_restore():
	await _wait_for_enemies()
	restore_game_state()

		
func restore_game_state():
	if not GameState.has_saved_state:
		return

	for enemy in get_tree().get_nodes_in_group("enemies"):
		var id = enemy.get_instance_id()
		if id in GameState.enemy_2_positions:
			enemy.global_position = GameState.enemy_2_positions[id]


func disable_target():
	# Turn OFF target
	if is_dead or invulnerable:
		return
		
	GameState.money += 1
	GameState.kills += 1
	is_dead = true
	sprite.visible = false
	set_physics_process(false)
	collision_shape.set_deferred("disabled", true)
	await get_tree().physics_frame
	timer.start(respawn_time)
func _on_timer_timeout():
	can_attack = true
	if is_dead:
		# Move first (no collisions yet)
		global_position = spawn_position

		# Restore collision settings
		collision_layer = original_layer
		collision_mask = original_mask

		# Reactivate visuals
		sprite.visible = true
		invulnerable = true
		# Re‑enable collision SAFELY
		await get_tree().physics_frame
		collision_shape.set_deferred("disabled", false)
		set_physics_process(true)
		is_dead = false
		
		await get_tree().create_timer(0.3).timeout
		invulnerable = false

	#heals door when not getting hit
	if not heal_cooldown:
		if door and global_position.distance_to(door.global_position) > 20:
			Global.health += 1
			if Global.health > 100:
				Global.health = 100
	else:
		heal_cooldown = true

@export var speedy: float = 100.0

var door: Node2D


@warning_ignore("unused_parameter")
func _physics_process(_delta):
	if door == null:
		return

	# Direction toward door
	var direction = (door.global_position - global_position).normalized()
	# Move enemy
	velocity = direction * speed
	move_and_slide()
	if sprite.visible == false:
		speed = 0
	if sprite.visible == true:
		speed = speedy
	GameState.enemy_2_positions = position


# enemy detects and attacks the door
	if door and global_position.distance_to(door.global_position) < 20:
		if can_attack:
			attack_door()
			Global.getting_hit = false

# enemy damages the door
func attack_door():
	heal_cooldown = true
	Global.health -= 1
	#print("hit")
	can_attack = false
	timer.start(1.5) # attack cooldown time
	if Global.health <= 0:
		get_tree().change_scene_to_file("res://scenes/game_over.tscn")



func _wait_for_enemies():
	while get_tree().get_nodes_in_group("enemies").is_empty():
		@warning_ignore("redundant_await")
		await get

In the function that causes enemies to respawn, I would add some code that randomizes the respawn coordinates.

To respawn multiple enemies at the same time and/or place, I would store dead enemies in an array, then make the respawn function respawn multiple of them at once. For that, the respawn code should be ‘outside’ of the enemies, not in their script.

I will get right on it! Thanks for the tips!

Create an instance for your enemy.

Add the instance to the scene tree.

Change the global position of the instance to your desired spawn point.

Is there an easier way i can have it choose between to values so the enemies can spawn between any point between (-26, -79) and (41, -79)? or do i just need to put every value between those for it to choose from?

You can use randi_range() to pick random integers within a range, or randf_range() for floats.

Thank you so much for the help!

You’re welcome! Best of luck with your project!