GameMaker Need help with "switch"

sinigrimi

Member
Sorry for the stupid question, but I need to know how to make the analogue && (and) from the if system in the "The" Switch "Statement" system. I seriously don’t know. I need to test 2-4 events at once, but I want to do this in the "switch" system
 

FrostyCat

Redemption Seeker
There is no direct analogue. Stop falling for switch fetish.
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 discrete set of possible values using == only (must be constant in GMS 1.x and above), 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 written using switch blocks.
 
If your switch looks like
Code:
switch(foo) {
  case: break;
  case: break;
  if (true) && (true) bar();
  case: break;
}
There are multiple reasons for not working. First, you need to test for cases. It has to be in the form of "case [x]: [code here] break;" to function properly. Second, the if ... and ... statement is not being checked correctly. It needs to be in between a case: and the break. So this is more what it should look like:
Code:
switch(foo) {
  case 0: break;
  case 1: if (true) && (true) bar(); break;
  case 2: break;
}
 
Top