How to change to a scene that was created while the game was running?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ChessieD

Currently I am working on a top-down 2D game where the player explores a randomly generated dungeon. When the dungeon is generated, each room is a premade scene with set entrances and based on the layout generated, the program will go to each room in the generated layout and place a premade room with the same ‘links’ (doors or tunnels).
I use gdscript by the way.
Instead of instancing each room in one big scene, I would like the whole scene to change to the room scene based on which room the player is in.
Currently all rooms are in 1 big scene with stand-in rooms and the camera changes to a camera fixed on the room the player is in for prototyping. If you imagine the Binding of Isaac with the rooms being blank with a white outline then that is exactly what it looks like right now.
The program DOES save what room is adjacent to it in a dictionary (replacing null if there is a room and staying null if there isn’t a room) like below:

var connectedRooms = {
    Vector2(1, 0): null,
    Vector2(-1, 0): null,
    Vector2(0, 1): null,
    Vector2(0, -1): null,

I just don’t know how to change scene as I cannot write ‘change scene to this path when enter here’ as the path I want the scene to change to technically hasn’t been made yet.
I was wondering if maybe I put something like ‘if enter this area (the exit) change scene to this room in dictionary’ but I don’t know exactly what code to put in.
I’m also worried that if I do do ‘if enter this area (the exit) change scene to this room in dictionary’ then I don’t want the character to be entering the same scene when it is actually two different rooms using the same room layout so would I have to do something with .duplicate()?
Thanks in advance.

:bust_in_silhouette: Reply From: Inces

I don’t think this is good way to go. Changing scene to each room will force You to save and load entire pattern of rooms visited before and overall room map. I would make scene overlay, scene of room wouldbe active, but a scene of whole corridor would be hidden and would not be freed.

Whatever You choose, the answer for question about dynamic scene instantiating is quite simple. Just create one scene of “room” and make it have variables indyvidualizing its layout in script. For example this pseudocode :
var r = preload(res//room)
var room = r.instance()
r.trapsamount = randomnumber * difficulty
r.doors = some number passed by corridor scene
r.monsters = etc etc
r.tilemap = randomly generate anew

Something like this. This can also be done with New() and init() constructor combo, no big difference

The reason I want to load each room is because the ‘room’ doesn’t necessarily only consist of one room and some will be about 4 viewports large. Is it still a good idea? I don’t know how difficult it is for the engine to load scenes. What is a scene overlay?

ChessieD | 2021-03-05 21:11

loading is not a problem for engine, You can eventually be worried about keeping both scenes active at the same time, which could be dangerous for framerate. This will not be a problem as long as You make sure that scene of whole corridor does not process anything while the camera is zoomed on one active room.

There is no such thing as scene overlay, I just used that word and explained what I mean :). And i will repeat - it is not to change scene form one to another, but keep both scenes, one is hidden, another is playable and zoomed by camera.
Take Heroes of Might and Magic for example. When combat happens, the world with castles and stuff don’t disappear, they are in the background. Scene is not CHANGED into combat, because every time combat occurs all that world information would have to be kept and recreated again after combat.

Inces | 2021-03-06 15:04