Sorry for the long answer, I wanted to solve this problem myself but I couldn’t. The current result is that when you start the rooms are randomly scattered around the scene and it’s not clear by what principle… My original idea was to have rooms drawn with a tilemap, and for the OGs to connect to the crydor (which is also essentially a room) via Marker2D, located on the “holes” in the rooms. But in the end, nothing works. I’m ready to change the original structure of the scenes or whatever it takes to make it work. I don’t understand anything, I’ll soon be tearing my hair out and banging my head against the wall. I would send a video but it tells me that new users can’t send videos. I don’t know how to become a non-new user.
my code(in separate scene "levelGenerateEtage1’):
extends Node2D
@export var rooms: Array[PackedScene]
@export var corridor_scene: PackedScene
var spawned_rooms: Array =
func _ready():
print(“Start”)
generate_level()
func generate_level():
if rooms.is_empty():
print(“No rooms assigned!”)
return
var max_rooms = 7
var first_room = spawn_room(Vector2.ZERO)
if not first_room:
print("Failed to spawn first room")
return
spawned_rooms.append(first_room)
while spawned_rooms.size() < max_rooms:
var parent_room = spawned_rooms.pick_random()
var parent_door = find_door(parent_room)
if parent_door:
var new_room = spawn_room(parent_door.global_position)
if new_room:
var new_door = find_door(new_room)
if new_door:
spawn_corridor(parent_door.global_position, new_door.global_position)
spawned_rooms.append(new_room)
func spawn_room(position: Vector2):
var room_scene = rooms.pick_random()
if not room_scene:
print(“No room scene found”)
return null
if not can_spawn_room(position):
return null
var room_instance = room_scene.instantiate()
add_child(room_instance)
room_instance.global_position = position
print("Spawned room at:", position)
return room_instance
func can_spawn_room(position: Vector2) → bool:
for room in spawned_rooms:
if room.global_position.distance_to(position) < 50:
return false
return true
func find_door(room):
if not room:
return null
var door = room.get_node_or_null("Door")
if door and door is Marker2D:
return door
print("No door marker found in", room.name)
return null
func spawn_corridor(start_pos: Vector2, end_pos: Vector2):
if not corridor_scene:
print(“No corridor scene assigned!”)
return
var corridor_instance = corridor_scene.instantiate()
add_child(corridor_instance)
corridor_instance.global_position = (start_pos + end_pos) / 2
print("Spawned corridor between:", start_pos, "and", end_pos)