• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Using switch with arrays

Hello, i'm having a bit of trouble on making my array work with the switch function.
I have my direction variables stored in a array in the create event like this:


GML:
dir = [
        direction <= 11.25 || direction >= 348.76,
        direction >= 11.26 && direction <= 33.75,
        direction >= 33.76 && direction <= 56.25,
        direction >= 56.26 && direction <= 78.75,
        direction >= 78.76 && direction <= 101.25,
        direction >= 101.26 && direction <= 123.75,
        direction >= 123.76 && direction <= 146.25,
        direction >= 146.26 && direction <= 168.75,
        direction >= 168.76 && direction <= 191.25,
        direction >= 191.26 && direction <= 213.75,
        direction >= 213.76 && direction <= 236.25,
        direction >= 236.26 && direction <= 258.75,
        direction >= 258.76 && direction <= 281.25,
        direction >= 281.26 && direction <= 303.75,
        direction >= 303.76 && direction <= 326.25,
        direction >= 236.26 && direction <= 348.75
    ]
Now in the step event i want to read these statements and return wich one i'm in.
I've been trying to do it like this:

Code:
    switch(dir) {
    
    case dir[0]: show_debug_message("It's on direction 0");
    case dir[1]: show_debug_message("It's on direction 1");
    
    break;
    }
And so on...

But it's not returning anything, i don't know the best way to do this so any ideas and directions will help a lot!
Thx in advance! 🙏
 

TailBit

Member
If I remember it correct, then you should be able to turn it into the direction number right away:
GML:
switch( ((direction + 11.25) div 22.5) mod 16 ){
    case 0: show_debug_message("It's on direction 0"); break;
    case 1: show_debug_message("It's on direction 1"); break;
}
But what you have made is just an array with a lot of true and false, as the operations are done when you fill the array

EDIT: or was it
((direction - 11.25 + 360) div 22.5) mod 16
 
If I remember it correct, then you should be able to turn it into the direction number right away:
GML:
switch( ((direction + 11.25) div 22.5) mod 16 ){
    case 0: show_debug_message("It's on direction 0"); break;
    case 1: show_debug_message("It's on direction 1"); break;
}
But what you have made is just an array with a lot of true and false, as the operations are done when you fill the array

EDIT: or was it
((direction - 11.25 + 360) div 22.5) mod 16
The thing is that if i do it using "if" statements it works normally, i need to verify these directions as true and false and then i'll tell the game wich sprite it should render changing along with these directions range.
 
I solved the problem, i just threw the array into step event and boom, done hahahaha
Basically i just wanted to get the boolean for wich is my current direction and having this array in the step event already does this 🙏

Thanks for the help anyways guys!
 

Attachments

Top