My switch statement is possessed o_O
by Stephen Triche · in Torque Game Builder · 06/22/2006 (12:49 pm) · 8 replies
I have the following bit of code
%testrandom = getRandom(1, 4);
echo(%testrandom);
switch (%testRandom)
{
case 1: do something;
case 2: do something;
case 3: do something;
case 4: do something;
default : do something that should never, ever, happen;
}
For some odd reason, default is happening.
%testrandom = getRandom(1, 4);
echo(%testrandom);
switch (%testRandom)
{
case 1: do something;
case 2: do something;
case 3: do something;
case 4: do something;
default : do something that should never, ever, happen;
}
For some odd reason, default is happening.
#2
06/22/2006 (1:33 pm)
No, you actually do need to have the break; statements in there. It signifies the end of the case. If I want something to happen on both 1 and 2, then I would do this:switch(%number) {
case 1:
case 2:
// Do the 1,2 thing
break;
case 3:
// Do the 3 thing
break;
default:
// Whatever
}
#3
06/22/2006 (1:49 pm)
Actually TorqueScript doesn't work the way most other languages switch statements do, it does not fall through to the next case. The break statements are not necessary.
#4
06/22/2006 (2:46 pm)
I think the break statement is dangerous if your switch is inside a for. while, loop... never tested though
#5
06/22/2006 (3:06 pm)
I believe Torque will throw a syntax error on breaks inside a switch statement, I could be wrong though. However, I do know for a fact that break statements are not necessary in TorqueScript switch statements.
#6
06/22/2006 (3:42 pm)
I stand corrected.
#7
06/22/2006 (3:47 pm)
I think it may not throw a syntax error but may actually break you switch statement... and not in the intended way :)
#8
06/22/2006 (4:02 pm)
It will give you a "break outside of a loop" error/warning at run time. It will compile just fine.
Torque Owner Stephen Triche
Default Studio Name