Godot Version
Godot 4.2.1 Stable
Question
I’m trying to create a grid of rooms, that spawn as the player gets close to them, and I’m trying to make it so the rooms don’t spawn on top of one another. I’m doing this by checking if a “RoomMarker” object, which sits a room lengths away from each room on each side, is colliding with any other rooms. Here is my current code in the RoomMarker object,
extends Marker3D
var room_scene = preload("res://room.tscn")
@onready var player = get_parent().get_parent().get_parent().get_node("Player")
@onready var room = $"../room"
@onready var area = $Area3D
var area_clear = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
if area.has_overlapping_areas() == true:
area_clear = false
else:
area_clear = true
var player_distance = player.global_transform.origin.distance_to(global_transform.origin)
if player_distance < 500 and area_clear == true:
var room_instance = room_scene.instantiate()
get_parent().get_parent().get_parent().add_child(room_instance)
room_instance.position = position
This however, will spawn rooms only in the direct neighbours to the spawn room, and it will spawn them infinitely, even though area.has_overlapping_areas() should be set to true by then, which should stop more rooms from spawning. Here is a picture of the room scene, including the RoomMarkers off to the side:
And here is what it looks like when I run the game:
Any help would be greatly appreciated. Thank you!