Simple case/select?
by Jeffrey T. Spicer · in Torque Game Builder · 06/29/2006 (4:01 pm) · 3 replies
I'm trying to randomly select a sound effect from 5 variations. What is wrong with my code?
%whicheffect = getRandom(1, 5);
if (%whichsound = 1)
{
alxPlay( soundeffect1 );
}
if (%whichsound = 2)
{
alxPlay( soundeffect2 );
}
if (%whichsound = 3)
{
alxPlay( soundeffect3 );
}
if (%whichsound = 4)
{
alxPlay( soundeffect4 );
}
if (%whichsound = 5)
{
alxPlay( soundeffect5 );
}
%whicheffect = getRandom(1, 5);
if (%whichsound = 1)
{
alxPlay( soundeffect1 );
}
if (%whichsound = 2)
{
alxPlay( soundeffect2 );
}
if (%whichsound = 3)
{
alxPlay( soundeffect3 );
}
if (%whichsound = 4)
{
alxPlay( soundeffect4 );
}
if (%whichsound = 5)
{
alxPlay( soundeffect5 );
}
#2
If you do compare, you need to use "==" not just "=".
Or after first if (%whichsound = 1) your variable is being set to 1.
for string comparing use if (%stringVar1 $= %stringVar2) { .. } routine
06/29/2006 (4:11 pm)
And... "what is wrong with your code".If you do compare, you need to use "==" not just "=".
Or after first if (%whichsound = 1) your variable is being set to 1.
for string comparing use if (%stringVar1 $= %stringVar2) { .. } routine
#3
One thing to note about TorqueScript switch statements: they do not cascade, meaning that one and only one case body will execute. C++ has what's called cascading cases, where you must explicitly indicate (via a break statement) when the compiler should stop branching through each case--Torquescript does not (and will error out in some fashion if you use a break statement of that nature within the case).
06/29/2006 (5:59 pm)
Bank hit both of my comments exactly!One thing to note about TorqueScript switch statements: they do not cascade, meaning that one and only one case body will execute. C++ has what's called cascading cases, where you must explicitly indicate (via a break statement) when the compiler should stop branching through each case--Torquescript does not (and will error out in some fashion if you use a break statement of that nature within the case).
Associate Fyodor -bank- Osokin
Dedicated Logic
switch (%whichsound ) { case 1: // Do something interesting. case 2: case 3: case 4: case 5: default: }for the string comparing use switch$ { ... }but in your case I'll be doing it like so:
%tmp = "soundeffect" @ getRandom(1, 5);
alxPlay( %tmp );
this should work.
First, you make the required string