The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

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.

in Engine by (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.

by (22,674 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.