• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Switch Statement Compile Error

C

Cofefe

Guest
Hi all!

So today I thought I'd try something with Switch statements, I figured it wouldn't work but I didn't expect the error I got. All I did was put the "Or" Operator in the cases (||). You can see the code below:
Code:
///@description horizontal_tile_collisions()

var grounded = tile_place_below(tile, 1);
x += xsp;
if(xsp > 0)
{
    switch(tile_place_right(tile, 0))
    {
        case 0:
            //Nothing
            break;
        
        case (1 || 2 || 4 || 5):
            x = (bbox_right & ~15) - 1 - sprite_bbox_right;
            xsp = 0;
            break;
            
        case (3 || 6 || 7):
            y = (bbox_bottom & ~15) - sprite_bbox_bottom;
            break;
    }
    if(grounded != 0) && (tile_place_below(tile, 1) = 0) && (tile_place_below(tile, xsp) = 0) y += xsp;
}
if(xsp < 0)
{
    switch(tile_place_left(tile, 0))
    {
        case 0:
            //Nothing
            break;
        
        case (1 || 3 || 6 || 7):
            x = ((bbox_left + 16) & ~15) + 1 - sprite_bbox_left;
            xsp = 0;
            break;
            
        case (2 || 4 || 5):
            y = (bbox_bottom & ~15) - sprite_bbox_bottom;
            break;
    }
    if(grounded != 0) && (tile_place_below(tile, 1) = 0) && (tile_place_below(tile, -xsp) = 0) y -= xsp;
}
And the compile error I got:
Code:
Script: horizontal_tile_collisions at line 18 : duplicate case statement found
Script: horizontal_tile_collisions at line 13 : original here
Script: horizontal_tile_collisions at line 37 : duplicate case statement found
Script: horizontal_tile_collisions at line 32 : original here
As you can see, it is not a duplicate case statement. So, IDK why I got this error.
I'm just gonna replace that with if statements, like I had before, but if somebody could explain this to me I'm interested, or if its a bug I can report it. Thanks!
 

rIKmAN

Member
If you want multiple cases to run the same code if true, you can do the following:
Code:
switch(thing)
{
    case 0:
    case 1:
        // do something if thing is 0 or 1
        break;

    case 2:
    case 3:
    case 4:
        // do something if thing is 2, 3 or 4
        break;
}
 
C

Cofefe

Guest
Ohh, sweet! Thanks for the tip. Still a weird error though. :confused:
 
Well,(1 || 2 || ...) is true, and so is (3 || 6 || 7). There's your error explained
Just to explain a little clearer. OR-ing numbers will return either true or false. When you use actual numbers in code, it gets compressed during compilation.
case (1 || 2 || 4 || 5):
and
case (3 || 6 || 7):

Both just compile down to:
case true:
case true:

So you have a duplicate case.
 
Top