• 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!

Legacy GM How to specify the value between one and the other value in the Switch statement

E

Edwin

Guest
It sounds incomprehensible, but I'll try to explain...
Code:
switch (hp) {
        case (LOWER OR EQUALS 50)
}
Is it possible to set value in case that lower or greater (< or >), 'cause I want to make like when HP is lower than 50, sprite changes to another one.
I know that I can use "if" statement, but if "switch" is capable of using "<>" stuff in cases, please tell me how to do it.
If it is not possible, sorry for wasting your time and troubling you. :p
 

Simon Gust

Member
You can try this
Code:
switch (hp <= 50)
{
case true: break;
case false: break;
}
I doubt there is a gain because the evaluation happens already before the switch.
so
Code:
if (hp <= 50)
{

}
else
{

}
Is better.

You can also use an array if you have a constant max hp.
Say you have 100 max hp, you can make an array like this
Code:
sprite[0] = spr_player_low_health;
sprite[1] = spr_player_high_health;

spr = sprite[hp div 51];
 

Binsk

Member
Switch statements are designed for checking specific values not ranges. If you need to check a range then is an if statement.
 

Neptune

Member
I have a feeling you're trying to segment your object's HP to gain more control.
Try making an enumerated value for it.
Code:
enum HP
{
low,
med,
high,
max,
}
Code:
if hp < 20 {global.hp_enum = HP.low;}
else if hp >= 20 && hp < 50 {global.hp_enum = HP.med;}
else if hp >= 50 && hp < 100 {global.hp_enum = HP.high;}
else {global.hp_enum = hp.max;}
And then everywhere else in your code...
Code:
if global.hp_enum == HP.high { /* do stuff */}

switch(global.hp_enum)
{
case HP.low: /* do stuff */ break;
case HP.med: /* do stuff */ break;
//etc
}
 
Top