Game Development Community

Switch$ bug

by Jonathon Stevens · in Torque Game Engine · 02/01/2006 (9:06 pm) · 9 replies

When I use the switch$ statement, it doesn't work. Am I doing something wrong? Here is an example of how I write it:

%myVariable = "test";

switch$(%myVariable)
{
   case "blah":
      %results = "found blah";
   case "test":
      %results = "found test";
   default:
      %results = "found didly squat";
}

I've tried both double AND single quotes and neither ever works. It always goes to default even when it's "blah" or "test".

About the author

With a few casual games under his belt as CEO of Last Straw Productions, Jonathon created the increasingly popular Indie MMO Game Developers Conference.


#1
02/01/2006 (11:06 pm)
Syntax looks correct to me.
Works for me, too.
#2
02/02/2006 (7:53 am)
Strange. When I mouse over %myVariable it says 'test' yet when it runs through the switch$ it goes to default every time.

On a side note, is their any string functions such as toLower, toUpper, trim, etc. for TS?
#3
02/02/2006 (8:27 am)
Not sure about scripting, but shouldn't there be a break; or something?!
Otherwise it runs "down" until the default...
#4
02/02/2006 (8:28 am)
Sure is. (:

Quote:
strlwr - converts string to lowercase
strupr - converst string to uppercase
ltrim - left trim
rtrim - right trim
trim - trim
#5
02/02/2006 (8:33 am)
TorqueScript switch statements do not need "break"s after each case.

Edit: One of the problems I have seen with the switch statements in TS is that you seem to need separate code for each case. So in other wordsm if I want the same code to fire for different "case"s, I have to replicate the code for each one. So this code:

switch (%test) {
     case 1:
     case 2:
          echo("hi there");
}

will not echo the text for both case 1 and case 2. This is needed:

switch (%test) {
     case 1:
          echo("hi there");
     case 2:
          echo("hi there");
}

I had always thought that the first method was acceptable.

Edit Again:
Never mind. Turns out the two things are related. The first method would only work if TS required "break" statements. Since those are implied for each "case", that method won't work.
#6
02/02/2006 (8:36 am)
Just read it in the tdn ;)
#7
02/02/2006 (11:20 am)
Yea, TDN says you don't need the 'break' statements. They are acceptable to have, but unnecesary.

thanks stefan for those functions =)
#8
02/03/2006 (8:59 am)
I never realized that Torque Script has a switch statement just for strings. At first I thought that was your problem. I'm guessing that you need to convert your string first to either upper or lower then compare to upper or lower since TDN states "TorqueScript will only execute matching cases."
#9
02/03/2006 (9:52 am)
Could it be that your local variable %myVariable isn't being passed because it is a local Var?