Making a Spawner that takes into account the Player's and SpawnedEntities position

Godot Version



I want my Spawner to spawn a coin every few seconds but I want to make it so that the coins cannot spawn within a certain radius of each other or near the player. What would be the best way to go about this?

This is my current Spawner script:

extends Node2D

@export var spawnerinfo : SpawnerInfo

@export var playerinfo : PlayerInfo

@export var currency_scene : PackedScene

@onready var curr_spawn_timer = $CurrencySpawn

@onready var coin_spawn_sound = $CoinSpawnSound

var spawn_pos : Vector2

var rng =
var pos_rng_x : int
var pos_rng_y : int

var do_spawn : bool = false

var rng_x_same_as_player : bool = false
var rng_y_same_as_player : bool = false

func _ready():

func _process(delta):
	if do_spawn:
		for currency in spawnerinfo.curr_amount:
			var coin = currency_scene.instantiate()
			pos_rng_x = rng.randi_range(spawnerinfo.pos.x, spawnerinfo.pos.y)
			pos_rng_y = rng.randi_range(spawnerinfo.pos.x, spawnerinfo.pos.y)
			if (pos_rng_x > (Misc.player_pos.x + spawnerinfo.player_pos_border.x) && pos_rng_x < (Misc.player_pos.x + spawnerinfo.player_pos_border.y)):
				rng_x_same_as_player = true
			elif (pos_rng_y > (Misc.player_pos.y + spawnerinfo.player_pos_border.x) && pos_rng_y < (Misc.player_pos.y + spawnerinfo.player_pos_border.y)):
				rng_y_same_as_player = true
			while (rng_x_same_as_player):
				pos_rng_x = rng.randi_range(spawnerinfo.pos.x, spawnerinfo.pos.y)
			spawn_pos.x = pos_rng_x
			spawn_pos.y = pos_rng_y
			coin.position = spawn_pos
		do_spawn = false

func _on_currency_spawn_timeout():
	do_spawn = true

I am also aware that the current script is bad, I had two if’s to check if the coin would spawn near the player, but I forgot that the if would check only once so that is why I added a while loop, which I didn’t finish for some reason