Switch : if nothing meets condition do this?

B

BlueCat6123

Guest
I've searched around for a bit, but I could not find anything about checking if a switch() statement has no met conditions. I know that this
Code:
switch(var{
case 0: break:
case 1: break:
case 2: break:
case 3: break:
} else { }
doesn't work, specifically the 'else' part. Should I just resort to if statements or am I overthinking it? For context I'm making a Minecraft-esque command prompt that can run in-game and run commands (I sorted them with switch).
 

samspade

Member
Close but not quite. For 'else' behavior, you should use the default:

Code:
switch (variable) {
    case 0: break;
    case 1: break;
    case 2: break;
    case 3: break;
    default:
}
Noticed that I also changed it to a semi-colon after break, not a colon.

For more examples:


Manual
Switch Examples
 

rIKmAN

Member
I've searched around for a bit, but I could not find anything about checking if a switch() statement has no met conditions. I know that this
Code:
switch(var{
case 0: break:
case 1: break:
case 2: break:
case 3: break:
} else { }
doesn't work, specifically the 'else' part. Should I just resort to if statements or am I overthinking it? For context I'm making a Minecraft-esque command prompt that can run in-game and run commands (I sorted them with switch).
Use the default: case which is the block that will be run if no other cases match.

Manual page: https://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/14_language_features.html

Example:
Code:
switch (<expression>)
{
case <expression1>: <statement1>; ... ; break;
case <expression2>: <statement2>; ... ; break;
...
default: <statement>;
}
This works as follows:
  • First the expression is executed.
  • Next it is compared with the results of the different expressions after each of the case statements.
  • The execution continues after the first case statement with the correct value, until a break statement is encountered.
  • If no case statement has the right value, execution is continued after the default statement (it is not required to have a default statement, in which case no action will be taken).
edit: ninja'd
 

Nidoking

Member
Another neat thing you can do with default is that it doesn't have to be the last label in the switch, so if you've got a bunch of cases and you want one of them to be the default if nothing else works, you can do this:
Code:
switch (var)
{
case 0:
// do stuff
break;
default:
case 1:
// do stuff that would also happen if no case matches
break;
case 2:
// do stuff
break;
}
 

rIKmAN

Member
Another neat thing you can do with default is that it doesn't have to be the last label in the switch, so if you've got a bunch of cases and you want one of them to be the default if nothing else works, you can do this:
Code:
switch (var)
{
case 0:
// do stuff
break;
default:
case 1:
// do stuff that would also happen if no case matches
break;
case 2:
// do stuff
break;
}
Just to clarify for OP - you can do that with any case, not just default.
Code:
switch(var)
{
    case 0:
    case 1:
        // do stuff if var is 0 or 1
        break;

    case 2:
        // do stuff if var is 2
        break;

    case 3:
    case 4:
    case 5:
        // do stuff if var is 3, 4 or 5
        break;
}
 
Another neat thing you can do with default is that it doesn't have to be the last label in the switch, so if you've got a bunch of cases and you want one of them to be the default if nothing else works, you can do this:
Code:
switch (var)
{
case 0:
// do stuff
break;
default:
case 1:
// do stuff that would also happen if no case matches
break;
case 2:
// do stuff
break;
}
My concern with this, unless they changed something at some point, is that GM will see the default and execute that case without looking at any others below that, ie: case 2, rendering that case unreachable. Again, this might have changed, but last time I tried something like this, it would always run the default case over subsequent ones. Took a while to debug cause I wasnt expecting it.
 

Yal

šŸ§ *penguin noises*
GMC Elder
That's how switch statements works. The cases are compared to the switch'd value in the order they are written, the first matching value is the branch that is taken (and only that branch is taken, even if there's multiple matches). And default matches any value, so it will always be taken if encountered.
 

Nidoking

Member
Code:
switch(5)
{
    default:
    case 0:
        global.TestVar = 0;
        break;
        
    case 5:
        global.TestVar = 1;
        break;
}
Result: global.TestVar is 1

I highly recommend actually testing the things you intend to claim before posting them on a forum where people might read them.
 
Top