Plastic Gem #31: Torque Fubars for Noobs
by Anthony Rosenbaum · 07/23/2008 (7:05 am) · 1 comments
Download Code File

Three Fubars for Noobs to avoid
This article is for programmers new to Torque. Torque script is "idiosyncratic" (to say the least). It can even be downright misleading if you try to jump right in without reading any docs. Here are three things to watch out for
1. Scripting OOP is fake
You might notice that functions often have a first parameter called %this. It is good to note that the variable name %this is NOT intrinsic. Meaning you could choose any valid variable name for that parameter and it would still hold a reference to the object you are referring to. This also means if %this is not used as the first parameter, there is no implicit this built in, unlike the this reference found in many languages.
2. Arrays are fake
You might have heard this before on IRC or the forum and wondered what does it really mean. It means unlike in C where arrays are the starting address of many elements that are all grouped under one name , in Torque script array elements are really separate variables. You can refer to these variables as if they were a single array using the familiar [] array operator, but all that is really doing is contatenating the index onto the "array name". This means $array[0] is really the same thing as $array0, $array[1] is really the same thing as $array1, etc. When you do multidimensional arrays the "_" is also concatenated in there to keep the numbers separate. This means $array[0][0] is really $array0_0, etc.
Here is an example of this:
There are several useful side effects of this. One is that indices don't need to be contiguous. You could index with 0, 12, 127 and only those three variables need exist. None of the values between those numbers have to exist. In fact, since it is a cheat you dont even have to use a number as an index...a string with no spaces also works. This is used by the inventory system. The name of ammo datablocks are used as array indices.
3. Single quotes are tagged strings not characters
In most programming lanaguges single quotes indicate character data, however in Torque script single quoting a value makes it a Tagged string, which is a net-friendly compressed version of strings you will use often between server and client. You might have noticed that sometime when you echo a tagged string you get a number instead of the text it represents. In these cases you need to convert the taggesd string back to a normal string with the getTaggedString() function.

Three Fubars for Noobs to avoid
This article is for programmers new to Torque. Torque script is "idiosyncratic" (to say the least). It can even be downright misleading if you try to jump right in without reading any docs. Here are three things to watch out for
1. Scripting OOP is fake
You might notice that functions often have a first parameter called %this. It is good to note that the variable name %this is NOT intrinsic. Meaning you could choose any valid variable name for that parameter and it would still hold a reference to the object you are referring to. This also means if %this is not used as the first parameter, there is no implicit this built in, unlike the this reference found in many languages.
2. Arrays are fake
You might have heard this before on IRC or the forum and wondered what does it really mean. It means unlike in C where arrays are the starting address of many elements that are all grouped under one name , in Torque script array elements are really separate variables. You can refer to these variables as if they were a single array using the familiar [] array operator, but all that is really doing is contatenating the index onto the "array name". This means $array[0] is really the same thing as $array0, $array[1] is really the same thing as $array1, etc. When you do multidimensional arrays the "_" is also concatenated in there to keep the numbers separate. This means $array[0][0] is really $array0_0, etc.
Here is an example of this:
$var[0] = "zero"; $anotherVar[1][0] = "one zero"; echo($var0 SPC $anotherVar1_0);
There are several useful side effects of this. One is that indices don't need to be contiguous. You could index with 0, 12, 127 and only those three variables need exist. None of the values between those numbers have to exist. In fact, since it is a cheat you dont even have to use a number as an index...a string with no spaces also works. This is used by the inventory system. The name of ammo datablocks are used as array indices.
3. Single quotes are tagged strings not characters
In most programming lanaguges single quotes indicate character data, however in Torque script single quoting a value makes it a Tagged string, which is a net-friendly compressed version of strings you will use often between server and client. You might have noticed that sometime when you echo a tagged string you get a number instead of the text it represents. In these cases you need to convert the taggesd string back to a normal string with the getTaggedString() function.
%var = 'this is tagged'; echo(%var); %normal = getTaggedString(%var); echo(%normal);
About the author
Torque 3D Owner Novack
CyberianSoftware