random scene selection

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

hi i have a question, i am new to godot and i have 4 tscn files and when i press key i want it to randomly select one of these scenes how can i do that exactly?

:bust_in_silhouette: Reply From: SteveSmith

var new_scene_class
var idx = RandomNumberGenerator.new().randi_range(1, 4)
if idx == 1:
new_scene_class = preload(“myscene1.tscn”)

var new_scene = new_scene_class.instance()