+1 vote

Is it somehow possible to check for a range with the match statement?


match myVariable:
   0 - 9:
     Do this.
   10 - 99:
     Do that.
Godot version 3.2.3
in Engine by (45 points)

3 Answers

+1 vote

No, match checks for matches, not ranges.

by (8,548 points)

True. But the idea of adding support for ranges and negations has been proposed before and is still in discussion: https://github.com/godotengine/godot-proposals/issues/994

0 votes

I caught myself in a situation where this feature would be really really useful and save me some time and lines. I hope they implement it or something related to it. If they do and someone knows it, tell us about it, please!

by (14 points)
+1 vote

match can't do this, but if statement would be equally space-eficcient :

 if range(0,9).has(x):
by (8,101 points)

Also, You can introduce an array with borders of your ranges:

var borders = [0,10,100,267]

and check what index would your variable get if it was sorted into this array. Then You would be able to match the result

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.