You could make yourself a Node2D that represents a "door" from one room to the next. If the player walks through one, they go to the corresponding room, at the door marked to lead where you came from.
There are a handful of 2D Nodes that you can use to have editor-only display, or you can simply make it run visible = false
inside a _ready.
To set the room a door leads to, you can export a variable. The easy way is to export a String, but an enum would avoid typing errors:
var destination: String
tool # To allow _get_property_list to work in the editor
func _get_property_list() -> Array: # Allows to make custom exports
return [{
name = "destination",
type = TYPE_STRING,
hint = PROPERTY_HINT_ENUM,
hint_string = list_rooms()
}]
func list_rooms() -> String: # Simply lists the filenames in a folder
var room_scene_folder := "res://"
var directory := Directory.new()
directory.open(room_scene_folder)
directory.list_dir_begin(true)
var rooms := ""
var name := directory.get_next()
while name:
rooms += name
name = directory.get_next()
if name:
rooms += ","
return rooms
To convert the room name to a PackedScene simply get back the full path (room_scene_folder.plus_file(destination)
), and call load on that.
To find back which door you "came through", you can have them all register on _ready to an autoload, (MyAutoload.my_array.append(self)). And then teleport the player to it. Just make sure to clear the Array when a room is unloaded
To detect the player entering a door, you can use Area2D and have them press a button when colliding, or put two Areas, and detect when both are entered in short succession/the right order. The order would be a regular exported bool.