so i was trying to write my own roomgeneration system where i would put a script in every room and that would generate a room with that script, but it only ended up generating one room and 80 bugs, so i decided to make a new one from scratch.
so what i was envisioning for the roomgen is that i would have some room scenes, and i would randomly generate them.
extends Node2D
@onready var door_exit: Marker2D = $DoorExit
var roomscene1 = preload("res://Scenes/room_1.tscn")
var roomscene2 = preload("res://Scenes/room_2.tscn")
var randroom
var room
func _ready():
randroom = randi_range(1,2)
var previous_door = door_exit.global_position
if randroom == 1:
room = roomscene1.instantiate()
elif randroom == 2:
room = roomscene2.instantiate()
add_child(room)
var new_door = room.get_node("Door")
room.global_position = previous_door - new_door.position
but it only seems to generate one room, and i tried asking here already but nothing seemed to work, so i decided to make a new one from scratch. can someone teach me how scene based room generation works?
You are missing a loop of some kind.
What you are doing now in plain English is:
- choose a random number (1 or 2)
- if it’s 1, generate room 1
- if it’s 2, generate room 2
So you need a for-loop that says:
var amount_of_rooms = 10
for room in amount_of_rooms:
# And then add your room generating code here for 1 room,
# which the loop will do 10 times, so you have 10 rooms.
but i also have this same script on every single room so that when it generates a room, that room generates another. i also put on local to scene in all of the scripts in the scenes
It would be better to have a different node that handles room generation and put the room generation in there.
avoid using preload for large resources and scenes, you may be relying on a cyclical reference of which preload cannot create.
it is best to load as needed
extends Node2D
@onready var door_exit: Marker2D = $DoorExit
var roomscene1: String = "res://Scenes/room_1.tscn"
var roomscene2: String = "res://Scenes/room_2.tscn"
func _ready():
var randroom: int = randi_range(1,2)
var previous_door: Vector2 = door_exit.global_position
var room: Node2D
if randroom == 1:
room = load(roomscene1).instantiate()
elif randroom == 2:
room = load(roomscene2).instantiate()
add_child(room)
var new_door = room.get_node("Door")
room.global_position = previous_door - new_door.position
However, do not run this code. I believe this will create an infinite loop. when add_child is called it will call the room’s _ready function, which as you stated all the scenes have the same ready function, thus another add_child call and the cycle repeats.
You have no way to stop this, and basing it off of ready is probably a bad idea, this also leads to each room only being connected to one other room. Using a “manager” node, this could have a while loop with a easily defined number of rooms/end.
# manager.gd
extends Node2D
const roomscene1: String = "res://Scenes/room_1.tscn"
const roomscene2: String = "res://Scenes/room_2.tscn"
func _ready():
var randroom: int = randi_range(1,2)
var previous_door: Vector2
for i in 20:
var room: Node2D
if randroom == 1:
room = load(roomscene1).instantiate()
elif randroom == 2:
room = load(roomscene2).instantiate()
add_child(room)
if previous_door:
var new_door: Node2D = room.get_node("Door")
room.global_position = previous_door - new_door.position
previous_door = room.door_exit.global_position
oh wait sorry my two brain cells didnt see the manager part