Changing to random scene

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

Hello! I’m making a game where once you click on the new game button, it sends you to a random level. (I have 10 total levels or scenes)

I was told to do something like this:

var random=randi()%9+1 

And then you would do something like this:
match random:

1: change_scene_to ( level1) #level1 is packed scene**
2: change_scene_to ( level2)

and so on…

So how would I implement this into my code? I put it in, and an error came up saying

Unexpected token: match:

How could I fix this?

Click HERE for code image.

:bust_in_silhouette: Reply From: jgodfrey

From the screenshot, it looks like that code lives outside a function. That won’t work…

At the very least, you need to wrap the code in a function and call it from somewhere…

func change_to_random_scene():
    var ranScene = randi() % 9 + 1
    match ranScene:

However, I’d probably store each scene reference in an array and then randomly select an element from that array to get a random scene. It’ll lead to less code maintenance as new scenes are added. But, what you have should generally work if it’s put into a function and called when needed.