Game Development Community

Interesting facts about TorqueScript

by Jason McIntosh · in Torque Game Builder · 03/08/2005 (8:07 pm) · 26 replies

Through some confusing trial and error (and contrary to the TorqueScript docs), switch statements do not need break statements after each case like C++ does.
switch ( expression )
{
    case 1: statement;
    case 2: statement;
    case 3: statement;
    case n: statement;
}
I have a switch statement inside a for loop, and the break was causing the for loop to stop. I would expect that from a break statement if it wasn't in a switch() statement. :)

Maybe knowing this will save you time when you come across this curious circumstance.

(Apart from these types of quirks, I'm really enjoying TorqueScript.)
Page «Previous 1 2
#1
03/08/2005 (8:24 pm)
Thanks for the heads up, though I've never come across this before in any docs or examples. But then I'm new to Torquescript...
#2
03/08/2005 (8:54 pm)
Which is really neat, except for when I WANT multiple cases to do the same thing, in which case it gets ugly.


Today's interesting fact about ++ and --;
the webpage
[after reading again] says that they're "post-fix" operators.

This is subtly different from post-increment, which is what I thought it meant before I re-read the page.

In other words, what you'd do like this in C++:

int i=0;
Object c[10];

for(int j=0; j<10; j++) {
c[i++] = new Object();
}

will in fact break horribly in torquescript.

It's a PRE-INCREMENT operator, it just happens to be grammatically a postfix token.
The same code in torquescript MUST be written:

%i=0;
for(%j=0;%j<10;%j++) {
%c[%i] = new ScriptObject(Object);
%i++;
}

ie, putting it as this:
%c[%i++] = new ScriptObject(Object);

will in fact fill %c[1] up to %c[11] with object handles, leaving %c[0] completely untouched.

Apologies for the swearing in the IRC channel when I finally figured this out today.

Gary (-;

PS I strongly feel that this should be documented, as the behaviour is directly contradictory to any other language that people might have used in the past.
#3
03/08/2005 (9:47 pm)
Whoa, you guys are on to something. There should be a thread totally dedicated to people posting interesting facts about TGE, T2D, and Torque script in general.

What do ya'll think?
#4
03/08/2005 (10:29 pm)
@Gary: Not to bash your style at all (to each his own), but I wish it forced you to put the increment in its own statement for every case. The code is much easier to follow in your second (TorqueScript correct) example without the ++ in the index brackets. I always prefer to have code you can read later rather than making it as "compact" as possible since a good optimizer will do that for you (in C++ anyway). :) And thanks for the info!

@Bryan: I plan to post any quirks like this that I find, so when there's a Wiki this stuff should definitely be in there.

Edit: I changed the name of this thread so we can get all those tasty TorqueScript idiosyncrasies documented here.
#5
03/08/2005 (10:50 pm)
Jason: actually, that code has some purposes that I like. Notably:

if(%something[%foo++]) {
echo("Something");
} else {
echo("Not Something");
}

As a post-increment, you have to put the foo++ inside both of the brackets, if you intend to use it in either of those blocks.

Gary (-;
#6
03/08/2005 (11:10 pm)
Add it to the unofficial faq.
#7
03/09/2005 (1:03 am)
Except, as I said, if you intend to use %foo in either of the brackets [ie, your code is even slightly less simple than mine], then you need to put it in both.

In the end, I guess it's a matter of personal taste.

Gary (-;
#8
03/09/2005 (1:03 am)
@Gary: you could also do this
%foo++;
if (%something[%foo-1]) {
  echo("Something");
} else {
  echo("Not Something");
}
#9
03/09/2005 (3:40 am)
You may find this Torque-Script initiative interesting too.

- Melv.
#10
03/09/2005 (12:10 pm)
I don't know if it's in that big-ass .plan, but here's one of my TorqueScript favorites:

$myArray1[1] = 5;
$myArray[11] = 10;
echo($myArray11);

What does it print? ;-)
#11
03/09/2005 (12:33 pm)
10. That's kind of confusing, but I guess it's not that often that you would number variable names like that anyway (thank the gods!). Unless you're doing some spaghetti coding with lots of globals and no structure... eeeeew.

Still, this does seem prone to errors, and since it's all legal syntax, there may not be any error report about it! EEEEEW.
#12
03/09/2005 (12:45 pm)
Ooooh, that one bites.

Gnah. So what's going on with the full-scale torquescript changes?

Gary (-;
#13
03/09/2005 (1:01 pm)
Does anyone even use the array reference without [ and ] ??? I can't believe it's part of the language! Same goes for using the _ char between subscripts.

Same goes for the huge difference between " and ' as string delimiters.

Is there a place to log deprecation requests? :-)


My #1 feature request for torquescript would be making the arrays work exactly like they do in PHP. Any variable or constant should be allowed as an array key. Hash arrays make life easy and code small and readable.

The more that torquescript looked and worked like PHP, the happier I would be, but I'm pretty biased :-)
#14
03/09/2005 (2:15 pm)
Ah. You're missing a specific leap of intuition; That arrays actually aren't:
%array[something] is a shorthand way of writing out the variable name
%arraysomething

Multidimensional arrays actually aren't, either:
%array[something,bar] is a shorthand way of writing out the varaible name %arraysomething_bar

I don't actually consider this to be a bad thing in general, as it gives you things like hash tables for free.

" and ' are different for an important and pivotal network-related reason, and are a Good Thing(TM).

Hash tables are also slow compared with simple munging to create a variable name.

Personally I use PHP a lot, too, but that doesn't mean that I think it's a better design. In fact I consider torquescript generally superior to php in a variety of ways from an academic perspective.

Gary (-;
#15
03/09/2005 (3:18 pm)
There's too many places where real arrays are needed though, and casting everything to a scalar reference just pollutes the namespace and makes a lot of things prohibitively expensive to do.

Now it makes sense why positional parameters are passed as space-separated strings. I'm liking the language less as I get to know it more, unfortunately.

I understand the " vs. ' over the network, but there's got to be dozens of better ways to differentiate the two.

I think I'm either going to try the python glue (even though I dislike python), or take a stab at doing the same glue with PHP. I know I won't be able to do without real arrays, especially ones of the hashed variety, once I get going full steam.
#16
03/09/2005 (3:50 pm)
Quote:I think I'm either going to try the python glue (even though I dislike python), or take a stab at doing the same glue with PHP. I know I won't be able to do without real arrays, especially ones of the hashed variety, once I get going full steam.

Then implement a hashed_array class, and/or use script objects.
#17
03/09/2005 (4:23 pm)
A php guy is complaining about namespace pollution? Come on.

... Globals aren't global until you define them locally as global... except some of them...

... Until very recently, register_globals was on by default which allows someone to arbitrarily stuff your global namespace with arbitrary variable names and values...


In the end, there isn't any namespace pollution if you pick a scheme and run with it.

Always use the notation %something[a,b], and you'll never even know that there's a problem. a and b are allowed to be expressions that get evaluated.

Passing positional parameters as space-separated strings is very useful; you only have one variable to pass, and it's just a variable, not an object. It's trivial to create and trivial to parse, and being typeless makes it printf-debugging-friendly.

And since when are torquescript arrays "prohibitively expensive"? They're computationally *free*; your lexer does all of the work already, unlike a hash function which would have to grind some CPU. In torquescript, both hashes and arrays are computationally free. PHP can't even pretend to do that.


I'm not actually trying to bash PHP, I think it's a pretty good language for webby things, but that doesn't make it non-crap from an academic perspective, and it certainly doesn't make it better than torquescript for working with the game engine for which it was designed.

Gary (-;
#18
03/09/2005 (6:10 pm)
When null isn't null at all...

Here's something I didn't expect -

I write a ConsoleFunction which allowed one of its parameters to be null. When I invoked it in script, I passed null as one of its parameters.

A couple hours of bug hunting later, I found out that null was NOT being passed into my ConsoleFunction as an actual null-zero value, but instead, it was being passed in as the string "null"!

So, when writing ConsoleFunctions for Torque Script, make sure to check for the null string in the optional parameters. And when invoking ConsoleFunctions from script, be sure to pass "" instead of null for safety!
#19
03/09/2005 (6:18 pm)
PS - I vote we not turn a good thread into a language war :)
#20
03/09/2005 (6:25 pm)
Aye. Language wars are bad.

Gary (-;
Page «Previous 1 2