Game Development Community

Switch statements and Cases

by Drymetal · in Torque Game Builder · 04/21/2012 (3:32 am) · 6 replies

I am taking the fish demo tutorial and I just have a question to make sure I'm following the script correctly. Are Switch statements and cases similar to If and Elseif statements in PHP? I mean at the end of the day, do they do the same sort of things? For instance, in PHP I might have:

if (in_array($zip, $Nashville)) {
 $my_email = "email1@email.com";

	}elseif (in_array($zip, $Knoxville)) {
 $my_email = "email2@email.com";

} else { $my_email = "default@email.com";

}

So it is basically saying IF this is true then do this. Or IF this is true do this. Otherwise do this.

The switch statement seems to do this very thing. My hope is - that instead of trying to memorize a bunch of new rules - I'll be able to relate some of them to things I already know in PHP and hopefully making learning easier. Anyways - Thanks.

#1
04/21/2012 (2:12 pm)
Yes, a switch is pretty much just a "cleaner" bunch of else ifs.

switch(%zip)
{
    case $Nashville:
        $my_email = "email1@email.com";
    case $Knoxville:
        $my_email = "email2@email.com";
    default:
        $my_email = "default@email.com";
}

Either way works, and if I remember correctly switch statements in torquescript aren't FASTER than long chains of else ifs, like they would be in C++... so feel free to use those if you're more comfortable that way.

Also -- use "switch$()" instead if you're testing a string rather than a number!
#2
04/21/2012 (2:25 pm)
Yeah that does look a lot cleaner. And actually a little easier to get around. I've gotten into a bunch of other tutorials and I'm starting to see a whole lot of similarities between torquescript and php. That's pretty cool. I've always been wary of learning another language because I *thought* they would all be so entirely different from one another.

My code editing program seems to think torquescript is C#. Is it based on that or something? And if so - is C# similar to php?

Thanks for the response!
#3
04/21/2012 (2:38 pm)
Yeah Torquescript is a "C-like" language, and so is PHP, so they'll look similar.

Also C++, Java, C#, and a bunch of others.

Oh -- and if you are looking to do some Torquescripting -- you can't go wrong with Torsion.

step-through debugging in Torquescript = super useful!


#4
04/21/2012 (3:31 pm)
Yeah I'm going to have to buy that. Good on pointing Torsion out.

I bought Torque 3D Pro and the 2D one. I've been working in the 2D but looking across this site and the internet - it looks like there is a lot more documentation for the 3D version. Perhaps I should focus on that first. <- Which means I'll have to buy the upgrade to 1.2 or whatever also.

Do you have any thoughts on the most efficient way to get a grasp of all this? I don't necessarily have any dreams of making games - rather...I'm just really, really sick of making websites every day. :)
#5
04/23/2012 (7:06 am)
Not really sure what the best way is to start out and build it up, really... There are some tutorials for T2D, but I'm not sure if they work "out of the box" in their current form, because I haven't tried 'em.

3D will be a much more complicated animal than 2D... unless you want to make something very VERY close to what is included with the engine (a basic shooter). Even then, remember there will be art that needs doing!

#6
04/23/2012 (7:56 pm)
My suggestion would be to make up a simple goal and try to grind out a routine that does it. Adding a score to the Fish Tutorial is a good one (from your other thread). Hopefully you'll learn some stuff as you try to get it to work right :)

If you don't have something that you really want to do already, you could just build on that. Try to add more features and make the Fish Demo into more of a real game.