Randomizing Main Scene

Godot Version

Godot 4.2

Question

I am looking to have my game pick from a selection of different 3D scenes as a background for a main menu. I’m unsure of where to start or how to search up the question because I am not sure if that’s even possible. From what I could gather, there is no way to change the Main Scene outside of declaring it in the Project Settings. But I could very well be wrong.

Thanks!

You need to be more clear with you objective, you want to change the entire scene or just want to pick a random scene and apply as background? Because both can be done but with different results. When we talk about change the entire scene we’re talking change from a stage to another as example.

I want to pick a random scene from, say, 3 choices as a background for the main menu, yes.

Edit: The same idea as how Portal 2 has different visuals for their main menu depending on where they are in the story, just random.

# Create an array and preload all the background you want to use,
# put the path where they saved, this is just an example
var my_backgrounds_preload := [
	preload("res://my_background_01"), 
	preload("res://my_background_02"), 
	preload("res://my_background_03")
]

func _ready() -> void:
	# Randomize the engine seed
	randomize()

	# Pick a random preloaded scene
	var new_background = my_backgrounds_preload.pick_random()

	# Create a new instance and add to your current scene
	var background_instance = new_background.instantiate()
	add_child(background_instance)

	# If needed, adjust the position, in this case the value can vary
	# acordyling your scene size and camera size, try without this first
	# and check the result
	background_instance.position = Vector(100, 100)

That’s exactly what I needed! Thank you for the help, this makes perfect sense.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.