0
\$\begingroup\$

Okay so I am making a tower defense game in GML and I came across a small problem when making a switch statement, I want to have anything from 1-10 to result in a variable changing to 1, anything from 11-20 result in the variable changing to 2, and anything 21-30 to change to 3. It would look something like this. Yes I know I'm new to coding..

switch(test)
{
    case (1-10):
        test = 1;
        break;

    case (11-20):
        test = 2;
        break;

    case (21-30):
        test = 3;
        break;
}

Except for the fact that that does not work and to actually do something like that, in my knowledge you'd have to make a case for every single number in the range which would take ~3 years. Help?

\$\endgroup\$
4
  • \$\begingroup\$ switch statements don't allow anything like this. They are meant to evaluate several values at once, though you can do the same with an if...else cascade. Also, you want to evaluate real values, not integer ones, and to do so you need to use logical operators such as < or >= and so on, if...else statement is your choice. BTW this is not GM:S related question, this is basic programming. \$\endgroup\$
    – liggiorgio
    Commented Sep 9, 2016 at 16:16
  • \$\begingroup\$ @liggiorgio "switch" statements are not a universally similiar programming concept; some languages allow their use in different ways than others (for example, C# allows string constants whereas C++ doesn't). So it's important for the language context to be provided; in this case that language is apparently GML. \$\endgroup\$
    – user1430
    Commented Sep 9, 2016 at 16:50
  • \$\begingroup\$ @JoshPetrie What you say is correct, I acknowledge there are different switch statement implementations around there. On the other hand, they have one property in common: the only comparison operation they perform is equality between the argument and the case values. It isn't possible to specify a value range for numbers, hence it's preferable to use a certain number of if...else statements. \$\endgroup\$
    – liggiorgio
    Commented Sep 9, 2016 at 21:23
  • \$\begingroup\$ Thank you all for the quick relipes :) I've got it worked out now \$\endgroup\$
    – Tanner H.
    Commented Sep 10, 2016 at 22:55

5 Answers 5

1
\$\begingroup\$

According to the documentation it looks like the switch works like it does in other languages and needs to have values known at compiletime, which means you can't use an expression as a case in a switch.
But there is a way to do a switch with a range and that is by skipping the break in some of the case statements. That can become quite tedius and it is probably simpler to use if else, especially in your exemple where it will look like this.

if(test > 0 and test <= 10)
{
    test = 1;
} 
else if (test > 10 and test <= 20)
{
    test = 2;
}
else if (test > 20 and test <= 30)
{
    test = 3;
}

But if you really want to use a switch it will look something like this

switch(test)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
   test = 1;
   break;
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
   test = 2;
   break;
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
   test = 3;
   break;
}
\$\endgroup\$
1
  • \$\begingroup\$ just to add to your answer. The cases do not need to be on a new line. They could be case 1: case 2: case 3: etc. \$\endgroup\$
    – Uri Popov
    Commented Sep 9, 2016 at 8:19
3
\$\begingroup\$

You should use if:

if test < 11
{
    test = 1;
}
else if test < 21
{
    test = 2;
}
else if test < 31
{
    test = 3;
}

Another example with different ranges:

if test >= 5 and test <= 10
{
    test = 1;
}
else if test >= 15 and test <= 17
{
    test = 2;
}
else
{
    test = 3;
}
\$\endgroup\$
0
\$\begingroup\$

Instead of using a switch or an if-else, you can attempt a math-based approach.

test = floor((test + 9) / 10);

\$\endgroup\$
2
  • \$\begingroup\$ then test = (test + 9) div 10 \$\endgroup\$
    – Dmi7ry
    Commented Sep 9, 2016 at 19:40
  • \$\begingroup\$ Yea, integer division is what I was looking for. \$\endgroup\$
    – ndenarodev
    Commented Sep 9, 2016 at 20:47
0
\$\begingroup\$

In example if you don't need 1 last digit you can use "div" operator - div, mod (%) - Division and modulo, where div gives you the amount a value can be divided into producing only an integer quotient, while mod gives you only the remainder of a division. Examples of use: secs = time mod 60; time_str = string(time div 60);

\$\endgroup\$
-1
\$\begingroup\$
key = (test-1) div 10

switch (key) {
    case 1:
    break;

    case 2:
    break;

    case 3:
    break;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .