# TorqueScript Syntax {#TorqueScriptSyntax} ## Main Rules Like other languages, TorqueScript has certain syntactical rules you need to follow. The language is very forgiving, easy to debug, and is not as strict as a low level language like C++. Observe the following line in a script: ~~~{.cpp} // Create test variable with a temporary variable %testVariable = 3; ~~~ The three most simple rules obeyed in the above code are: 1. Ending a line with a semi-colon (;) 2. Proper use of white space. 3. Commenting The engine will parse code line by line, stopping whenever it reaches a semi-colon. This is referred to as a statement termination, common to other programming languages such as C++, Javascript, etc. The following code will produce an error that may cause your entire script to fail: ~~~{.cpp} %testVariable = 3 %anotherVariable = 4; ~~~ To the human eye, you are able to discern two separate lines of code with different actions. Here is how the script compiler will read it: ~~~{.cpp} %testVariable = 3%anotherVariable = 4; ~~~ This is obviously not what the original code was meant to do. There are exemptions to this rule, but they come into play when multiple lines of code are supposed to work together for a single action: ~~~{.cpp} if(%testVariable == 4) echo("Variable equals 4"); ~~~ We have not covered conditional operators or echo commands yet, but you should notice that the first line does not have a semi-colon. The easiest explanation is that the code is telling the compiler: "Read the first line, do the second line if we meet the requirements." In other words, perform operations between semi-colons. Complex operations require multiple lines of code working together. The second rule, proper use of whitespace, is just as easy to remember. Whitespace refers to how your script code is separated between operations. Let's look at the first example again: ~~~{.cpp} %testVariable = 3; ~~~ The code is storing a value (3) in a local variable (`%%testVariable`). It is doing so by using a common mathematical operator, the equal sign. TorqueScript recognizes the equal sign and performs the action just as expected. It does not care if there are spaces in the operation: ~~~{.cpp} %testVariable=3; ~~~ The above code works just as well, even without the spaces between the variable, the equal sign, and the 3. The whitespace rule makes a lot more sense when combined with the semi-colon rule and multiple lines of code working together. The following will compile and run without error: ~~~{.cpp} if(%testVariable == 4) echo("Variable equals 4"); ~~~ ## Comments The last rule is optional, but should be used as often as possible if you want to create clean code. Whenever you write code, you should try to use comments. Comments are a way for you to leave notes in code which are not compiled into the game. The compiler will essentially skip over these lines. There are two different comment syntax styles. The first one uses the two slashes, `//`. This is used for single line comments: Example: ~~~{.cpp} // This comment line will be ignored // This second line will also be ignored %testVariable = 3; // This third line will also be ignored ~~~ In the last example, the only line of code that will be executed has to do with `%%testVariable`. If you need to comment large chunks of code, or leave a very detailed message, you can use the `/*comment*/` syntax. The `/*` starts the commenting, the `*/` ends the commenting, and anything in between will be considered a comment. Example: ~~~{.cpp} /* While attending school, an instructor taught a mantra I still use: "Read. Read Code. Code." Applying this to Torque 2D development is easy: READ the documentation first. READ CODE written by other Torque developers. CODE your own prototypes based on what you have learned. */ ~~~ As you can see, the comment makes full use of whitespace and multiple lines. While it is important to comment what the code does, you can also use this to temporarily remove unwanted code until a better solution is found: Example: ~~~{.cpp} // Why are you using multiple if statements. Why not use a switch$? /* if(%testVariable == "Mich") echo("User name: ", %testVariable); if(%testVariable == "Heather") echo("User Name: ", %testVariable); if(%testVariable == "Nikki") echo("User Name: ", %testVariable); */ ~~~ # Variables ## Usage Now that you know the two most basic rules of writing code in TorqueScript, this is the best time to learn about variables. A variable is a letter, word, or phrase linked to a value stored in your game's memory and used during operations. Creating a variable is a one line process. The following code creates a variable by naming it and assigning a value: ~~~{.cpp} %localVariable = 3; ~~~ You can assign any type value to the variable you want. This is referred to as a language being type-insensitive. TorqueScript does not care (insensitive) what you put in a variable, even after you have created it. The following code is completely valid: ~~~{.cpp} %localVariable = 27; %localVariable = "Heather"; %localVariable = "7 7 7"; ~~~ The main purpose of the code is to show that TorqueScript treats all data types the same way. It will interpret and convert the values internally, so you do not have to worry about typecasting. That may seem a little confusing. After all, when would you want a variable that can store a number, a string, or a vector? You will rarely need to, which is why you want to start practicing good programming habits. An important practice is proper variable naming. The following code will make a lot more sense, considering how the variables are named: ~~~{.cpp} %userName = "Heather"; %userAge = 27; %userScores = "7 7 7"; ~~~ Earlier, I mentioned that TorqueScript is more forgiving than low level programming languages. While it expects you to obey the basic syntax rules, it will allow you to get away with small mistakes or inconsistency. The best example is variable case sensitivity. At some point in school you learned the difference between upper case and lower case letters. With variables, TorqueScript is not case sensitive. You can create a variable and refer to it during operations without adhering to case rules: ~~~{.cpp} %userName = "Heather"; echo(%Username); ~~~ In the above code, `%%userName` and `%%Username` are the same variable, even though they are using different capitalization. You should still try to remain consistent in your variable naming and usage, but you will not be punished if you slip up occasionally. ## Variable Types There are two types of variables you can declare and use in TorqueScript: local and global. Both are created and referenced similarly: ~~~{.cpp} %localVariable = 1; $globalVariable = 2; ~~~ As you can see, local variable names are preceded by the percent sign `(%%)`. Global variables are preceded by the dollar sign `($)`. Both types can be used in the same manner: operations, functions, equations, etc. The main difference has to do with how they are scoped. In programming, scoping refers to where in memory a variable exists and its life. A local variable is meant to only exist in specific blocks of code, and its value is discarded when you leave that block. Global variables are meant to exist and hold their value during your entire programs execution. Look at the following code to see an example of a local variable: ~~~{.cpp} function test() { %userName = "Heather"; echo(%userName); } ~~~ We will cover functions a little later, but you should know that functions are blocks of code that only execute when you call them by name. This means the variable, `%%userName`, does not exist until the test() function is called. When the function has finished all of its logic, the `%%userName` variable will no longer exist. If you were to try to access the %%userName variable outside of the function, you will get nothing. Most variables you will work with are local, but you will eventually want a variables that last for your entire game. These are extremely important values used throughout the project. This is when global variables become useful. For the most part, you can declare global variables whenever you want: ~~~{.cpp} $PlayerName = "Heather"; function printPlayerName() { echo($PlayerName); } function setPlayerName() { $PlayerName = "Nikki"; } ~~~ The above code makes full use of a global variable that holds a player's name. The first declaration of the variable happens outside of the functions, written anywhere in your script. Because it is global, you can reference it in other locations, including separate script files. Once declared, your game will hold on to the variable until shutdown. ## Data Types TorqueScript implicitly supports several variable data-types: numbers, strings, booleans, and arrays and vectors. If you wish to test the various data types, you can use the echo(...) command. For example: ~~~{.cpp} %meaningOfLife = 42; echo(%meaningOfLife); $name = "Heather"; echo($name); ~~~ The echo will post the results in the console, which can be accessed by pressing the ctrl+tilde key (`~`) while in game. ## Numbers TorqueScript handles standard numeric types ~~~{.cpp} 123 (Integer) 1.234 (floating point) 1234e-3 (scientific notation) 0xc001 (hexadecimal) ~~~ ## Strings Text, such as names or phrases, are supported as strings. Numbers can also be stored in string format. Standard strings are stored in double-quotes. ~~~{.cpp} "abcd" ~~~ Example: ~~~{.cpp} $UserName = "Heather"; ~~~ Strings with single quotes are called "tagged strings." ~~~{.cpp} 'abcd' (tagged string) ~~~ Tagged strings are special in that they contain string data, but also have a special numeric tag associated with them. Tagged strings are used for sending string data across a network. The value of a tagged string is only sent once, regardless of how many times you actually do the sending. On subsequent sends, only the tag value is sent. Tagged values must be de-tagged when printing. You will not need to use a tagged string often unless you are in need of sending strings across a network often, like a chat system. Example: ~~~{.cpp} $a = 'This is a tagged string'; echo(" Tagged string: ", $a); echo("Detagged string: ", detag('$a')); ~~~ The output will be similar to this: ~~~{.cpp} 24 ___ ~~~ The second echo will be blank unless the string has been passed to you over a network. ## String Operators There are special values you can use to concatenate strings and variables. Concatenation refers to the joining of multiple values into a single variable. The following is the basic syntax: ~~~{.cpp} "string 1" operation "string 2" ~~~ You can use string operators similarly to how you use mathematical operators (=, +, -, *). You have four operators at your disposal: **String Operators** | Operator | Name | Example | Explanation | |-------------|:-------------:|:-------------:|------------| | @ | String concatenation | `$c @ $d` | Concatenates strings $c and $d into a single string. Numeric literals/variables convert to strings. | | NL | New Line | `$c NL $d` | Concatenates strings $c and $d into a single string separated by new-line. | | TAB | Tab | `$c TAB $d` | Concatenates strings $c and $d into a single string separated by tab. | | SPC | Space | `$c SPC $d` | Concatenates strings $c and $d into a single string separated by space. Note: such a string can be decomposed with getWord() | The `@` symbol will concatenate two strings together exactly how you specify, without adding any additional whitespace: Note: Do not type in OUTPUT: ___. This is placed in the sample code to show you what the console would display Example: ~~~{.cpp} %newString = "Hello" @ "World"; echo(%newString); OUTPUT: HelloWorld ~~~ Notice how the two strings are joined without any spaces. If you include whitespace, it will be concatenated along with the values: Example: ~~~{.cpp} %newString = "Hello " @ "World"; echo(%newString); OUTPUT: Hello World ~~~ String operators work with variables as well: Example: ~~~{.cpp} %hello = "Hello "; %world = "World"; echo(%hello @ %world); OUTPUT: Hello World ~~~ The rest of the operators will apply whitespace for you, so you do not have to include it in your values: Example: ~~~{.cpp} echo("Hello" @ "World"); echo("Hello" TAB "World"); echo("Hello" SPC "World"); echo("Hello" NL "World"); OUTPUT: HelloWorld Hello World Hello World Hello World ~~~ ## Booleans Like most programming languages, TorqueScript also supports Booleans. Boolean numbers have only two values- true or false.
true (1) false (0)Again, as in many programming languages the constant "true" evaluates to the number 1 in TorqueScript, and the constant "false" evaluates to the number 0. However, non-zero values are also considered true. Think of booleans as "on/off" switches, often used in conditional statements. Example: ~~~{.cpp} $lightsOn = true; if($lightsOn) echo("Lights are turned on"); ~~~ ## Arrays Arrays are data structures used to store consecutive values of the same data type. ~~~{.cpp} $TestArray[n] (Single-dimension) $TestArray[m,n] (Multidimensional) $TestArray[m_n] (Multidimensional) ~~~ If you have a list of similar variables you wish to store together, try using an array to save time and create cleaner code. The syntax displayed above uses the letters 'n' and 'm' to represent where you will input the number of elements in an array. The following example shows code that could benefit from an array: Example: ~~~{.cpp} $firstUser = "Heather"; $secondUser = "Nikki"; $thirdUser = "Mich"; echo($firstUser); echo($secondUser); echo($thirdUser); ~~~ Instead of using a global variable for each user name, we can put those values into a single array: Example: ~~~{.cpp} $userNames[0] = "Heather"; $userNames[1] = "Nikki"; $userNames[2] = "Mich"; echo($userNames[0]); echo($userNames[1]); echo($userNames[2]); ~~~ Now, let's break the code down. Like any other variable declaration, you can create an array by giving it a name and value: ~~~{.cpp} $userNames[0] = "Heather"; ~~~ What separates an array declaration from a standard variable is the use of brackets []. The number you put between the brackets is called the index. The index will access a specific element in an array, allowing you to view or manipulate the data. All the array values are stored in consecutive order. If you were able to see an array on paper, it would look something like this: ~~~{.cpp} [0] [1] [2] ~~~ In our example, the data looks like this: ~~~{.cpp} ["Heather"] ["Nikki"] ["Mich"] ~~~ Like other programming languages, the index is always a numerical value and the starting index is always 0. Just remember, index 0 is always the first element in an array. As you can see in the above example, we create the array by assigning the first index (0) a string value ("Heather"). The next two lines continue filling out the array, progressing through the index consecutively. ~~~{.cpp} $userNames[1] = "Nikki"; $userNames[2] = "Mich"; ~~~ The second array element (index 1) is assigned a different string value ("Nikki"), as is the third (index 2). At this point, we still have a single array structure, but it is holding three separate values we can access. Excellent for organization. The last section of code shows how you can access the data that has been stored in the array. Again, you use a numerical index to point to an element in the array. If you want to access the first element, use 0: ~~~{.cpp} echo($userNames[0]); ~~~ In a later section, you will learn about looping structures that make using arrays a lot simpler. Before moving on, you should know that an array does not have to be a single, ordered list. TorqueScript also supports multidimensional arrays. An single-dimensional array contains a single row of values. A multidimensional array is essentially an array of arrays, which introduces columns as well. The following is a visual of what a multidimensional looks like with three rows and three columns: ~~~{.cpp} [x] [x] [x] [x] [x] [x] [x] [x] [x] ~~~ Defining this kind of array in TorqueScript is simple. The following creates an array with 3 rows and 3 columns. Example: ~~~{.cpp} $testArray[0,0] = "a"; $testArray[0,1] = "b"; $testArray[0,2] = "c"; $testArray[1,0] = "d"; $testArray[1,1] = "e"; $testArray[1,2] = "f"; $testArray[2,0] = "g"; $testArray[2,1] = "h"; $testArray[2,2] = "i"; ~~~ Notice that we are are now using two indices, both starting at 0 and stopping at 2. We can use these as coordinates to determine which array element we are accessing: ~~~{.cpp} [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2] ~~~ In our example, which progresses through the alphabet, you can visualize the data in the same way: ~~~{.cpp} [a] [b] [c] [d] [e] [f] [g] [h] [i] ~~~ The first element `[0,0]` points to the letter 'a'. The last element `[2,2]` points to the letter 'i'. ## Vectors "Vectors" are a helpful data-type which are used throughout Torque 2D. For example, many fields on SceneObjects take numeric values in sets of 2 or 3. These are stored as strings and interpreted as "vectors". ~~~{.cpp} "1.0 1.0" (2 element vector) ~~~ The most common example of a vector would be a world position. Like most 2D coordinate systems, an object's position is stored as `(X Y)`. You can use a two element vector to hold this data: Example: ~~~{.cpp} %position = "25 32"; ~~~ You can separate the values using spaces or tabs (both are acceptable whitespace). Another example is storing color data in a four element vector. The values that make up a color are "Red Blue Green Alpha," which are all numbers. You can create a vector for color using hard numbers, or variables: Example: ~~~{.cpp} %firstColor = "100 100 100 1.0"; echo(%firstColor); %red = 128; %blue = 255; %green = 64; %alpha = 1.0; %secondColor = %red SPC %blue SPC %green SPC %alpha; echo(%secondColor); ~~~ # Operators Operators in TorqueScript behave very similarly to operators in real world math and other programming languages. You should recognize quite a few of these from math classes you took in school, but with small syntactical changes. The rest of this section will explain the syntax and show a brief example, but we will cover these in depth in later guides. **Arithmetic Operators** | Operator | Name | Example | Explanation | |:-------------:|:-------------:|:-------------:|------------| | * | multiplication | `$a * $b` | Multiply $a and $b. | | / | division | `$a / $b` | Divide $a by $b. | | % | modulo | `$a % $b` | Remainder of $a divided by $b. | | + | addition | `$a + $b` | Add $a and $b. | | - | subtraction | `$a - $b` | Subtract $b from $a. | | ++ | auto-increment (post-fix only) | $a++ | Increment $a. The value of $a++ is that of the incremented variable: auto-increment is post-fix in syntax, but pre-increment in sematics (the variable is incremented, before the return value is calculated). This behavior is unlike that of C and C++. | | \-- | auto-decrement (post-fix only) | $b\-- | Decrement $b. The value of $a\-- is that of the decremented variable: auto-decrement is post-fix in syntax, but pre-decrement in sematics (the variable is decremented, before the return value is calculated). This behavior is unlike that of C and C++. | **Relational Operators** Used in comparing values and variables against each other. Relations can be arithmetic, logical, and string: | Operator | Name | Example | Explanation | |:-------------:|:-------------:|:-------------:|------------| | `<` | Less than | `$a < $b` | 1 if $a is less than $b (0 otherwise.) | | `>` | More than | `$a > $b` | 1 if $a is greater than $b (0 otherwise.) | | `<=` | Less than or Equal to | `$a <= $b` | 1 if $a is less than or equal to $b (0 otherwise.) | | `>=` | More than or Equal to | `$a >= $b` | 1 if $a is greater than or equal to $b (0 otherwise.) | | `==` | Equal to | `$a == $b` | 1 if $a is equal to $b (0 otherwise.) | | `!=` | Not equal to | `$a != $b` | 1 if $a is not equal to % b (0 otherwise.) | | `!` | Logical NOT | `!$a` | 1 if $a is 0 (0 otherwise.) | | `&&` | Logical AND | `$a && $b` | 1 if $a and $b are both non-zero (0 otherwise.) | | `$=` | String equal to | `$c $= $d` | 1 if $c equal to $d . | | `!$=` | String not equal to | `$c !$= $d` | 1 if $c not equal to $d. | One additional operator is the logical OR, represented by `||`. Example: ~~~{.cpp} $a || $b ~~~ OR will return 1 if either `$a` or `$b` is non-zero (0 otherwise.) **Bitwise Operators** Used for comparing and shifting bits | Operator | Name | Example | Explanation | |:-------------:|:-------------:|:-------------:|------------| | ~ | Bitwise complement | ~$a | flip bits 1 to 0 and 0 to 1. (i.e. ~10b == 01b) | | & | Bitwise AND | $a & $b | composite of elements where bits in same position are 1. (i.e. 1b & 1b == 1b) | | ^ | Bitwise XOR | $a ^ $b | composite of elements where bits in same position are opposite. (i.e. 100b & 101b == 001b) | | << | Left Shift | $a << 3 | element shifted left by 3 and padded with zeros. (i.e. 11b << 3d == 11000b) | | >> | Right Shift | $a >> 3 | element shifted right by 3 and padded with zeros. (i.e. 11010b >> 3d == 00011b) | One additional operator is the bitwise OR, represented by `|`. Example: ~~~{.cpp} $a | $b ~~~ This will return composite of elements where bits 1 in either of the two elements. (i.e. `100b & 001b == 101b`). **Assignment Operators** Used for setting the value of variables. | Operator | Name | Example | Explanation | |:-------------:|:-------------:|:-------------:|------------| | Assignment | $a = $b; | Assign value of $b to $a. | Note: the value of an assignment is the value being assigned, so $a = $b = $c is legal. | | op= | Assignment Operators | $a op= $b; | Equivalent to $a = $a op $b, where op can be any of: `*` `/` `%` `+` `-` `&` `^` `<<` `>>` \| | # Control Statements TorqueScript provides basic branching structures that will be familiar to programmers that have used other languages. If you are completely new to programming, you use branching structures to control your game's flow and logic. This section builds on everything you have learned about TorqueScript so far. ## if, then, else This type of structure is used to test a condition, then perform certain actions if the condition passes or fails. You do not always have to use the full structure, but the following syntax shows the extent of the conditional: ~~~{.cpp} if(