Game Development Community

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 );
}

#1
06/29/2006 (4:08 pm)
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
#2
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
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).