Operators
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
These are your basic math ops.
Operator | Name | Example | Explanation |
---|---|---|---|
| multiplication |
| Multiply |
| division |
| Divide |
| modulo |
| Remainder of |
| addition |
| Add |
| subtraction |
| Subtract |
| auto-increment (post-fix only) |
| Increment |
| auto-decrement (post-fix only) |
| Decrement |
++$a
is illegal. 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++.
--$b
is illegal. 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.
Operator | Name | Example | Explanation |
---|---|---|---|
| Less than |
| 1 if |
| More than |
| 1 if |
| Less than or Equal to |
| 1 if |
| More than or Equal to |
| 1 if |
| Equal to |
| 1 if |
| Not equal to |
| 1 if |
| Logical NOT |
| 1 if |
| Logical AND |
| 1 if |
| Logical OR |
| 1 if either |
| String equal to |
| 1 if |
| String not equal to |
| 1 if |
Bitwise Operators
Used for comparing and shifting bits.
Operator | Name | Example | Explanation |
---|---|---|---|
| Bitwise complement |
| flip bits 1 to 0 and 0 to 1 |
| Bitwise AND |
| composite of elements where bits in same position are 1 |
| Bitwise OR |
| composite of elements where bits 1 in either of the two elements |
| Bitwise XOR |
| composite of elements where bits in same position are opposite |
| Left Shift |
| element shifted left by 3 and padded with zeros |
| Right Shift |
| element shifted right by 3 and padded with zeros |
Assignment Operators
Used for setting the value of variables.
Operator | Name | Example | Explanation |
---|---|---|---|
| Assignment |
| Assign value of |
| Assignment Operators |
| Equivalent to |
The value of an assignment is the value being assigned, so
$a = $b = $c
is legal.
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:
You can use string operators similarly to how you use mathematical operators (=, +, -, *). You have four operators at your disposal:
Operator | Name | Example | Explanation |
---|---|---|---|
| String concatenation |
| Concatenates strings $c and $d into a single string. Numeric literals/variables convert to strings. |
| New Line |
| Concatenates strings $c and $d into a single string separated by new-line. Such a string can be decomposed with getRecord() |
| Tab |
| Concatenates strings $c and $d into a single string separated by tab. Such a string can be decomposed with getField() |
| Space |
| Concatenates strings $c and $d into a single string separated by space. Such a string can be decomposed with getWord() |
Miscellaneous Operators
General programming operators.
Operator | Name | Example | Explanation |
---|---|---|---|
| Conditional |
| Evaluates to |
| Array element |
| Synonymous with |
| Delimiting, Grouping |
| Argument list for function call Used with if, for, while, switch keywords Control associativity in expressions |
| Compound statement |
| Delimit multiple statements, optional for if, else, for, while Required for switch, datablock, new, function |
| Listing |
| Delimiter for arguments |
| Namespace |
| This definition of the |
| Field/Method selection |
| Select a console method or field |
| Single-line comment |
| Used to comment out a single line of code |
| Multi-line comment |
| Used to comment out multiple consecutive lines
|
There is no “comma operator”, as defined in C/C++;
$a = 1, $b = 2;
is a parse error.
Last updated