Why is my procedural generation script generating random invisible rooms?

Godot Version

v4.6.1

Question

I’m trying to procedurally generate a dungeon for a rougelike game im planning to make, but when i generate the dungeon it generates random colliders that i cant see when looking for collision shapes. Please forgive my bad code, I’m still rather new to gdscript.

My proc-gen script and its location:

extends Node2D
 
@export var _dimensions : Vector2i = Vector2i(7, 5)
@export var _start : Vector2i = Vector2i(-1, 0)
@export var _critical_path_length : int = 13
@export var _branches : int = 3
@export var _branch_length : Vector2i = Vector2i(1, 4)

var rooms = [
	preload("res://OtherScenes/room_1.tscn")
]
var roomsize = Vector2(540, 360)
var _branch_candidates : Array[Vector2i]
var dungeon : Array
 
func _ready() -> void:
	_initialize_dungeon()
	_place_entrance()
	_generate_path(_start, _critical_path_length, "C")
	_generate_branches()
	for x in range(_dimensions.x):
		for y in range(_dimensions.y):
			if cellhasroom(x, y):
				spawnroomat(x, y)
	_print_dungeon()
	
 
func _initialize_dungeon() -> void:
	for x in _dimensions.x:
		dungeon.append([])
		for y in _dimensions.y:
			dungeon[x].append(0)
 
func _place_entrance() -> void:
	if _start.x < 0 or _start.x >= _dimensions.x:
		_start.x = randi_range(0, _dimensions.x - 1)
	if _start.y < 0 or _start.y >= _dimensions.y:
		_start.y = randi_range(0, _dimensions.y - 1)
	dungeon[_start.x][_start.y] = "S"
 
func _generate_path(from : Vector2i, length : int, marker : String) -> bool:
	if length == 0:
		return true
	var current : Vector2i = from
	var direction : Vector2i
	match randi_range(0, 3):
		0:
			direction = Vector2i.UP
		1:
			direction = Vector2i.RIGHT
		2:
			direction = Vector2i.DOWN
		3:
			direction = Vector2i.LEFT
	for i in 4:
		if (current.x + direction.x >= 0 and current.x + direction.x < _dimensions.x and
			current.y + direction.y >= 0 and current.y + direction.y < _dimensions.y and
			not dungeon[current.x + direction.x][current.y + direction.y]):
			current += direction
			dungeon[current.x][current.y] = marker
			if length > 1:
				_branch_candidates.append(current)
			if _generate_path(current, length - 1, marker):
				return true
			else:
				_branch_candidates.erase(current)
				dungeon[current.x][current.y] = 0
				current -= direction
		direction = Vector2i(direction.y, -direction.x)
	return false
 
func _generate_branches() -> void:
	var branches_created : int = 0
	var candidate : Vector2i
	while branches_created < _branches and _branch_candidates.size():
		candidate = _branch_candidates[randi_range(0, _branch_candidates.size() - 1)]
		if _generate_path(candidate, randi_range(_branch_length.x, _branch_length.y), str(branches_created + 1)):
			branches_created += 1
		else:
			_branch_candidates.erase(candidate)
 
func spawnroomat(x : int, y : int):
	var roomselected = rooms.pick_random()
	var room = roomselected.instantiate()
	
	add_child(room)
	room.position = Vector2(x, y) * roomsize

	var north = y + 1 < _dimensions.y and cellhasroom(x, y + 1)
	var south = y - 1 >= 0 and cellhasroom(x, y - 1)
	var east  = x + 1 < _dimensions.x and cellhasroom(x + 1, y)
	var west  = x - 1 >= 0 and cellhasroom(x - 1, y)
	
	room.setdoors(north, south, east, west)
	print(room.global_position)
	print(room.get_global_transform())
func cellhasroom(x, y):
	return dungeon[x][y] is String
	
func _print_dungeon() -> void:
	var dungeon_as_string : String = ""
	for y in range(_dimensions.y - 1, -1, -1):
		for x in _dimensions.x:
			if dungeon[x][y]:
				dungeon_as_string += "[" + str(dungeon[x][y]) + "]"
			else:
				dungeon_as_string += "   "
		dungeon_as_string += '\n'
	print(dungeon_as_string)

The video shows that there is an invisible collider that is there for no reason.

My room’s script and its location:

extends Node2D

var closedtexture := preload("res://SpriteTextures/doorclosed.png")
var opentexture := preload("res://SpriteTextures/dooropen.png")

@onready var doors := {
	"north": $DoorNorth,
	"south": $DoorSouth,
	"west": $DoorWest,
	"east": $DoorEast,
}

func setdoors(n : bool, s : bool, e : bool, w : bool):
	setdoor("north", n)
	setdoor("south", s)
	setdoor("east", e)
	setdoor("west", w)
	
func setdoor(dir : String, enabled : bool):
	var node = doors[dir]
	
	if node:
		node.show()

func _on_static_body_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			var southdoor = doors["south"]
			
			if southdoor:
				var cs2d = southdoor.find_child("CollisionShape2D", true, false)
				var sprite = southdoor.find_child("Door", true, false)
				
				sprite.texture = opentexture
				cs2d.disabled = true

func _on_static_body_2d_2_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			var southdoor = doors["north"]
			
			if southdoor:
				var cs2d = southdoor.find_child("CollisionShape2D", true, false)
				var sprite = southdoor.find_child("Door", true, false)
				
				sprite.texture = opentexture
				cs2d.disabled = true


func _on_static_body_2d_3_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			var southdoor = doors["west"]
			
			if southdoor:
				var cs2d = southdoor.find_child("CollisionShape2D", true, false)
				var sprite = southdoor.find_child("Door", true, false)
				
				sprite.texture = opentexture
				cs2d.disabled = true

func _on_static_body_2d_4_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			var southdoor = doors["east"]
			
			if southdoor:
				var cs2d = southdoor.find_child("CollisionShape2D", true, false)
				var sprite = southdoor.find_child("Door", true, false)
				
				sprite.texture = opentexture
				cs2d.disabled = true

Okay now its not even generating anything visible.

Are you checking the “Remote” tab of your scene tree while the game is running?

Your “Room1” is set to not visible, this will hide all of it’s children too, but collision persists regardless of it’s visibility.

That was a mistake, it generated but it still has the invisible colliders in more place than just where room1 is located, and no I wasn’t.

EDIT: Ah, I found the issue, rooms are overlapping and are on top of each other, I still do not know how to fix this.