How to make objects go faster using switch statement?[solved]

Artwark

Member
Ok so I used this for a switch statement

Code:
switch(global.points)
{
case(10): speed = 2; break;//if global.points is == 10, speed increases by 2

case(30): speed = 4; break;//if global.points is == 30, speed increases by 4

case(50): speed = 6; break;//if global.points is == 50, speed increases by 6
}
What its suppose to do is that when global.points gives a value from 10 onwards, objects need to increase their speed from that point on. Problem is that the objects only do it when it reaches 10 and after it comes to 11, they go back to their normal speed. I want them to increase their speed from that point on.

I can easily fix this through if statement but I want to use switch since the objects will only increase their speed further when global.points reaches a certain value. Is there a way to do this using switch statement?
 

YoSniper

Member
I think that a switch/case is not the best choice for this. Instead, an if-else chain would work better.
Code:
if global.points < 10 {
    speed = 0;
} else if global.points < 30 {
    speed = 2;
} else if global.points < 50 {
    speed = 4;
} else {
    speed = 6;
}
 
K

kirkkaf13

Guest
Code:
switch(global.points)
{
case(10): speed += 2; break;//if global.points is == 10, speed increases by 2

case(30): speed += 4; break;//if global.points is == 30, speed increases by 4

case(50): speed += 6; break;//if global.points is == 50, speed increases by 6
}
Your statement is just setting the speed variable to the same value each time. You need to take the original speed and add to it.
 

Artwark

Member
Code:
switch(global.points)
{
case(10): speed += 2; break;//if global.points is == 10, speed increases by 2

case(30): speed += 4; break;//if global.points is == 30, speed increases by 4

case(50): speed += 6; break;//if global.points is == 50, speed increases by 6
}
Your statement is just setting the speed variable to the same value each time. You need to take the original speed and add to it.
It still won't increase the speed from that point on. If global.points increases to 11, it's resetting to its normal speed.

I want this:
Code:
if (global.points>=10)
{
speed = 2;
}
to be done using Switch statement. What you've given didn't do this that is unless I've done something wrong here.
 
N

Never Mind

Guest
Each case has to check for a constant value and cannot contain expressions / comparisons like >=.
You would have to use an if-else chain.
 
It still won't increase the speed from that point on. If global.points increases to 11, it's resetting to its normal speed.

I want this:
Code:
if (global.points>=10)
{
speed = 2;
}
to be done using Switch statement. What you've given didn't do this that is unless I've done something wrong here.
The Switch statement simply does not support comparisons, it's only to be used when there is a finite number of options. Use a chain of if-elseifs as suggested.
 

FrostyCat

Member
Recommended reading: How NOT to use switch
What can be converted to switch blocks and what can't

switch blocks are not universal silver bullets, stop treating them like one. Use switch only when you are matching an expression against a set of possible values using == only (must be constant in GMS 1.x), optionally plus a catch-all case.

In other words, only code in the following if-else ladder form have an equivalent switch block (final else optional):
Code:
var value = expression;
if (value == constant1) {
  //val1
}
else if (value == constant2) {
  //val2
}
//...
else {
  //default
}
Any code that cannot fit this form should not be changed into switch blocks.
Theoretically you can adapt the solution from Misguided Belief #3 to force switch to do it:
Code:
switch (global.points div 10) {
  case 0: 
    speed = 1; 
  break;
  case 1: case 2:
    speed = 2;
  break;
  case 3: case 4:
    speed = 4;
  break;
  default:
    speed = 6;
  break;
}
But given how poorly this solution generalizes, I am not inclined to call this a best practice. Just use an if-else ladder already and stop fetishizing over switch blocks.
 
Top