Ranges in Case statements?
by Novack · in Torque Game Engine · 06/17/2009 (5:52 pm) · 5 replies
Is there a native way to use ranges in the case staments?
Something like:
Thanks...
Something like:
switch(%value)
{
case 51 to 100:
%something = "A";
case -20 to 50:
%something = "B";
default:
%modifier = "C";
}Thanks...
About the author
http://cyberiansoftware.com.ar/
#3
Maybe its too specific, but the range() function can be easily cutomized. Hope it helps.
06/17/2009 (8:22 pm)
Coded a little hack to use ranges in the Case. A bit more clear than use multiples If, I think:switch(%value)
{
case range(%value, 51, 100):
%something = "A";
case range(%value, -20, 50):
%something = "B";
default:
%something = "C";
}
// ...
function range(%value, %min, %max)
{
if ((%value <= %max) && (%value >= %min))
return %value;
}Maybe its too specific, but the range() function can be easily cutomized. Hope it helps.
#4
06/17/2009 (8:31 pm)
haha, that's cool. I just wrote the exact same function!
#5
06/17/2009 (8:42 pm)
That is a praise!
Associate Jaimi McEntire
King of Flapjacks
if(%value >= 51 && %value <= 100) { %something = "A"; } else if (%value >= -20 && %value <= 50) { %something = "B"; } else { %modifier = "C"; }Switch docs:
The switch keyword is used to control the flow of a script. It is like the switch$ statement, but intended for numeric comparisons instead of string comparisons.
The general form of a switch statement is as follows:
switch ( numeric expression )
{
case value0:
statement(s);
break;
case value1:
statement(s);
break;
. . .
case valueN:
statement(s);
break;
default:
statement(s);
}
Execution
1. First, numeric expression is evaluated and compared with value0.
2. If the result is true, the statement(s) associated with that case are executed, and the switch statement is exited.
3. If not, control passes to the next case statement, where numeric expression is again evaluated, this time being compared to value1.
4. If control falls through to the default case, its statements will be executed.
switch is intended for use ONLY with numerical comparisons (==). If string comparisons are desired, use switch$.
Note: Unlike C/C++, the break statement is not for use in switch. TorqueScript will only execute matching cases and will NOT automatically execute all subsequent cases. This is proven in the example below.