Trying To Make A Fast Paced Roguelike Game

Godot Version

4.6.2

Question

Does Anyone Know How To Randomly Generate Scenes In A Single Scene One After Another

Like, I’ve Got 3 scenes consisting of 3 different rooms and i want the rooms spawn randomly when i go through one room to the other endlessly

like, the rooms will generate one after another randomly while i sprint through them

Can this be achieved using area and marker nodes???

I’m Pretty new to these stuff


I am also rather new to Godot and have no experience yet with interconnecting scenes yet, just switching between them. Is that what you want or do you want to do? If so it can be achieved with Marker3D and Area3D. I asked Gemini about this and this is what it came up with:

Yes, Area and Marker nodes are exactly the right tools for this job. It is a very common and efficient way to build an endless room or endless runner system in Godot.

Here is a straightforward architectural approach you can share with them, complete with the basic logic needed to make it work while keeping performance in check.

1. The Room Scene Setup

Each room should be its own separate scene. Inside each room scene, you need three key components:

  • The Geometry: The meshes, walls, floors, and collision shapes.

  • A Marker3D (The Exit Point): Place this node exactly where the next room should begin. It acts as the snap-point anchor.

  • An Area3D (The Trigger): Place this a little bit before the exit door. Make sure it has a CollisionShape3D covering the hallway. When the player walks through this, it signals the game to spawn the next room.

2. The Main Level Manager

Create a main scene (e.g., World or LevelManager) that will handle the spawning logic. This script needs an array to hold the room scenes and a reference to the starting point.

GDScript

extends Node3D

@export var room_scenes: Array[PackedScene]
var current_spawn_point: Vector3 = Vector3.ZERO

func _ready():
    # Spawn the very first room at the start
    spawn_random_room()

func spawn_random_room():
    # Pick a random room from the array
    var random_index = randi() % room_scenes.size()
    var room_instance = room_scenes[random_index].instantiate()
    
    # Add it to the world
    add_child(room_instance)
    
    # Move the room to the current spawn point
    room_instance.global_position = current_spawn_point
    
    # Connect the room's trigger Area3D to our script
    # Assuming the Area3D in the room scene is named "SpawnTrigger"
    var trigger = room_instance.get_node("SpawnTrigger")
    trigger.body_entered.connect(_on_player_entered_trigger.bind(room_instance))
    
    # Update the spawn point for the *next* room using this room's Marker3D
    # Assuming the Marker3D is named "NextRoomMarker"
    var marker = room_instance.get_node("NextRoomMarker")
    current_spawn_point = marker.global_position

3. Triggering the Next Room

When the player hits the Area3D, the connected function runs. It needs to verify that the player is the one triggering it, spawn the next room, and then disable the trigger so it doesn’t fire twice.

GDScript

func _on_player_entered_trigger(body: Node3D, room_node: Node3D):
    # Make sure it's the player triggering the area, not an enemy or item
    if body.is_in_group("player"):
        spawn_random_room()
        
        # Disable the trigger so walking back and forth doesn't spawn infinite rooms
        room_node.get_node("SpawnTrigger").queue_free()

4. Memory Management (The Crucial Step)

If the player sprints through rooms endlessly, the game will eventually crash from running out of memory. To handle this cleanly—similar to adding and removing data chunks—old rooms must be destroyed once the player is far enough away.

A simple way to handle this is to add a timer, or a second Area3D at the start of the room. When the player enters room 3, a signal tells room 1 to call queue_free(), instantly clearing those meshes and nodes from memory.

Sure, But I Don’t Want To Depend On AI For This

Is There Any Tutorials For This? If Yes,Then Pls Provide

And Thanks For The Clarification

Something like this will get you started:

Procedural Dungeon Generation - Godot 4

Ok, I find the recent AI models are quite good at making guides as well so I use that always to get the right ideas about what to do even if I do the actual coding myself. I think the code snippet above indicates what to do - add the marker and area. For the area you can see if it triggers correctly by just adding a Print in your script - I use that all the time to debug stuff and figure out when stuff is called. Adding the area in the room scene instead of adding it through code is always best for development as you get to tweak it all in the inspector too. Extending the area with a node inherited script lets you add extra settings to control what happens when you enter an area too where setting and controlling those parameters in the inspector makes it easy to experiment even while the game/code is running.