+6 votes

Does GDScript have switch or case statement?

asked Aug 12, 2016 in Engine by Chr_ (71 points)

2 Answers

–3 votes
Best answer

It's yet to be implemented, as you can see here, under Keywords.

answered Aug 12, 2016 by mateusak (1,025 points)
selected Aug 13, 2016 by Chr_

Is there any eta for when we can expect to see a Select Case statement for GDScript?

I don't think it's in 2.2 milestone, so it may take a long time.

We won't have switch, as there's already "match" in the current git master branch.
So you'll be able to use that in 3.0.

Is it done yet? If not, what alternatives or workarounds could we use?

+9 votes

The GDScript docs have the 'Match' keyword which appears to do the same thing...

A match statement is used to branch execution of a program. It’s the equivalent of the switch statement found in many other languages but offers some additional features.

answered Feb 16, 2017 by James Grimwood (32 points)

Yes, but keep in mind that you need the current git version to use it.
The next stable version that'll have it will be 3.0 :)

Edited -

Hey, "match" "switch" who cares what you call it, right? This is what I need to know. Thanks for clarifying.

How to cascade multiple cases for the same execution?? - so or like leaving out the breaks in Java like languages?

EDIT: JUST DUG DEEPER AND ANSWERED MY OWN QUESTION>>>>>

"
Multipatterns:
You can also specify multiple patterns separated by a comma. These patterns aren’t allowed to have any bindings in them.

match x:
1, 2, 3:
print("It's 1 - 3")
"Sword", "Splash potion", "Fist":
print("Yep, you've taken damage")

"

--Also if anyone wants to redirect me to a newer thread thats fine. But this thread always comes up the 10 other times I googled "godot match statement" recently trying to remember how to do it. So I thought it might as well need to be addressed better here, seeing that the google algorithm seems to favor it.

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.