0 votes

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: changesceneto ( level1) #level1 is packed scene**
2: changesceneto ( 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.

asked Jun 25 in Engine by Shrimped (14 points)

1 Answer

+1 vote

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.

answered Jun 25 by jgodfrey (5,742 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.