Game Development Community

Switch statement

by Bucko · in Torque Game Builder · 04/03/2005 (1:39 pm) · 3 replies

One nice feature seems* to be lacking in Torque script namely the ability to list multiple cases with common statements like in C++. The code below should call Echo("Clear sky tile caused collision!"); and return when %frame is 0-3 and Echo("Solid wall"); alxPlay(HitSound); when %frame is 4-7 but this doesn't happen. Do we have to write separate code for EVERY case that may come up?

// What frame nbr was it?
switch(%frame)
{
// Clear sky: bail
case 0:
case 1:
case 2:
case 3:
Echo("Clear sky tile caused collision!");
return;

// Solid wall
case 4:
case 5:
case 6:
case 7:
Echo("Solid wall");
alxPlay(HitSound);
}

*I've been missing some pretty obvious stuff lately so just humour me if this questionis stupid.

#1
04/03/2005 (2:09 pm)
Surely if the situation is that clear cut you could use:

if (%frame <= 3)
{ 
  Echo("Clear sky tile caused collision!");
}
else
{
  Echo("Solid wall");
  alxPlay(HitSound);
}

And then run into your Switch statements if you need to do specific things for each frame number.
#2
04/03/2005 (3:52 pm)
I've always been kinda... ambivalent about the way switch/case works in C/C++. On the one hand, having automatic fallthrough has its uses. The above case is pretty simple, but more interesting cases do exist.

On the other hand, it makes the common use of case statements more verbose (having to use break), and significant use of fallthrough makes reading and following the code more difficult.

If TScript's use in T2D were not as an attempt to fully replace C/C++ logic, then I would say that its current behavior is ideal. However, since TScript's use in T2D is supposed to fully replace C/C++ logic, I would argue that the language needs appropriate C/C++ constructs.
#3
04/04/2005 (11:28 pm)
There is no "fallthrough" in TorqueScript. I would like to have it, too, but it's not a show stopper. A little code replication won't hurt, especially if you minimize it by reducing it to a function call to the common code for each case.

Whether TS is meant to "totally replace" C++ is possibly debatable, but from a pragmatic point of view there's no reason TS should totally replace C++ since we all have the source code. Which makes debating it kind of unecessary.