Torque3D
  • General
    • Welcome!
    • Features
    • Release Notes
      • Version 4.0.3
      • Version 4.0.2
      • Version 4.0.1
      • Version 4.0
      • Version 3.10.1
      • Version 3.10
      • Version 3.9
      • Version 3.8
      • Version 3.7
      • Version 3.6.2
      • Version 3.6.1
      • Version 3.6
  • Getting Started
    • Introduction
      • What's the Torque3D Engine?
    • Getting Familiar
      • Getting a Copy
        • Torque Project Manager
        • Downloading it Yourself
      • Getting Ready for Launch
        • Running Pre-Built Binaries
        • Building the Engine Yourself
      • Launching the Game
        • Opening the Example Level
        • Launching the Editors
      • Your First Game
        • Introduction To The Engine
        • The Module System
        • Creating an empty gamemode
        • Adding a Player
        • Adding a Coin
        • Adding a win-condition
        • Custom coin asset
        • Counting coins
        • Adding some effects
        • Supporting multiplayer
        • Adding a Scoreboard GUI
        • Keeping the scoreboard up-to-date
      • Deep Dive: BaseGame Directory Structure
    • Best Practices
    • Porting a Legacy Project
  • For Artists
    • Assets
      • What are Assets?
      • How to Create a New Asset
      • Working With Assets
      • Deep Dive: Creating a New Asset Type
    • Art
      • File Formats
      • 3D Art
        • Shape Specifications
        • Coordinates System
        • Mounting Shapes
        • Animation
        • Player Setup
        • Blender -> Torque3D Pipeline
      • 2D Art
        • Working with Adobe Substance
    • Animation
    • GUI
      • Loading and Initializing a GUI
      • Expanding a GUI via Script
      • How to Network GUIs
    • Materials
      • Material Mapping
      • Material Animation
    • Terrain
    • Shaders
    • Lighting
    • Audio
  • For Designers
    • Base Classes
      • SimObject
      • SimGroup
      • SceneObject
      • Scene
      • Datablocks
    • Game Classes
      • Creating an Object
      • Destroying an Object
      • Gameplay Scripting
        • Spawning an Object from Gameplay Code
    • Modules
      • What are Modules?
      • How to Create a New Module?
      • Making a Module do Things
      • Installing Existing Modules
        • Where to Get More Modules
    • Scenes and Levels
      • How to Create a New Level
      • How to Load a Level
        • Deep Dive: Level Loading Scripts
      • How to Edit Levels
        • Opening a Level in the Editor
        • Spawning Objects from the Asset Browser
        • Working with Scenes
        • Using SimGroups
        • Changing a Level's PostEffects
        • Deep Dive: LevelAsset Companion Files
    • Game Modes
      • Creating a New GameMode
      • Making a Level Use Your GameMode
      • Adding Gameplay Code to Your GameMode
    • AI
      • Navmesh
      • Objects
      • Scripting
    • Inputs
      • Inputs and Keybinds
        • ActionMap
        • Bind Functions
        • ActionMap Stack
    • Localization
    • Editors
      • Changing Editor Settings
      • World Editor
        • Scene Editor
        • ConvexShape Editor
        • Terrain Editor
        • Terrain Painter
        • Material Editor
        • Spline-Based Tools
          • Mesh Road Editor
          • River Editor
          • Decal Road Editor
        • Datablock Editor
        • Particle Editor
        • Decal Editor
        • Forest Editor
        • Navmesh Editor
        • Deep Dive: Creating Your Own Editor
        • Shape Editor
      • GUI Editor
        • Interface Details
  • For Programmers
    • Compiling the Engine
      • Setup Development Environment
        • SDK and Library Installation
        • Git
        • Cmake
        • Creating a Fork on Github
      • Create a Project
        • Creating a Project With CMake
        • Creating a Project With the Project Manager
      • Compiling
        • Compiling in Windows
        • Compiling in Linux
        • Compiling in MacOS
      • Building the Project Manager
    • Introduction
    • Code Style Guidelines
    • Expanding the Engine
      • Creating a New Object Class
      • Exposing Object Classes to Script
        • addProtectedField
      • Adding a New Library to the Engine
    • Major Components of the Engine
      • Core
        • Console
        • Platform
      • Audio
        • SFX
      • Rendering
        • GFX
        • Render Bins
      • Physics
        • Stock T3D Physics
        • Physics Wrapper
          • PhysX
          • Bullet
        • Classes
    • Rendering
    • Math
    • Networking
      • Client and Server Commands
    • Physics
    • Collision
    • Scripting
      • TorqueScript
        • What is TorqueScript?
        • Basic Syntax
        • Variables
        • Types
        • Operators
        • Control Structures
        • Functions
        • Objects
        • Module Interop
          • QueueExec
          • RegisterDatablock
          • CallOnModules
          • ModuleExec
        • API Reference
      • Other Languages
        • C-Interface
    • File Inputs/Outputs(I/O)
    • API Reference
Powered by GitBook
On this page
  • Operators
  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Assignment Operators
  • String Operators
  • Miscellaneous Operators
Edit on GitHub
Export as PDF
  1. For Programmers
  2. Scripting
  3. TorqueScript

Operators

PreviousTypesNextControl Structures

Last updated 2 years ago

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

$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.

--

auto-decrement

(post-fix only)

$b--

Decrement $b.

Quote

++$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++.

Quote

--$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

$a < $b

1 if $a is less than $b

>

More than

$a > $b

1 if $a is greater than $b

<=

Less than or Equal to

$a <= $b

1 if $a is less than or equal to $b

>=

More than or Equal to

$a >= $b

1 if $a is greater than or equal to $b

==

Equal to

$a == $b

1 if $a is equal to $b

!=

Not equal to

$a != $b

1 if $a is not equal to $b

!

Logical NOT

!$a

1 if $a is 0

&&

Logical AND

$a && $b

1 if $a and $b are both non-zero

||

Logical OR

$a || $b

1 if either $a or $b is non-zero

$=

String equal to

$c $= $d

1 if $c equal to $d.

!$=

String not equal to

$c !$= $d

1 if $c not equal to $d.

Bitwise Operators

Used for comparing and shifting bits.

Operator
Name
Example
Explanation

~

Bitwise complement

~$a

flip bits 1 to 0 and 0 to 1

&

Bitwise AND

$a & $b

composite of elements where bits in same position are 1

|

Bitwise OR

$a | $b

composite of elements where bits 1 in either of the two elements

^

Bitwise XOR

$a ^ $b

composite of elements where bits in same position are opposite

<<

Left Shift

$a << 3

element shifted left by 3 and padded with zeros

>>

Right Shift

$a >> 3

element shifted right by 3 and padded with zeros

Assignment Operators

Used for setting the value of variables.

Operator
Name
Example
Explanation

=

Assignment

$a = $b;

Assign value of $b to $a

op=

Assignment Operators

$a op= $b;

Equivalent to $a = $a op $b, where op can be any of: * / % + - & | ^ << >>

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:

"string 1" operation "string 2"

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

$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. Such a string can be decomposed with getRecord()

TAB

Tab

$c TAB $d

Concatenates strings $c and $d into a single string separated by tab. Such a string can be decomposed with getField()

SPC

Space

$c SPC $d

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

x ? y : z

Evaluates to y if x equal to 1, else evaluates to z

[]

Array element

$a[5]

Synonymous with $a5

( )

Delimiting, Grouping

t2dGetMin(%a, %b)

if ( $a == $b )

($a+$b)*($c-$d)

Argument list for function call

Used with if, for, while, switch keywords

Control associativity in expressions

{}

Compound statement

if (1) {$a = 1; $b = 2;}

function foo() {$a = 1;}

Delimit multiple statements, optional for if, else, for, while

Required for switch, datablock, new, function

,

Listing

t2dGetMin(%a, %b)

%M[1,2]

Delimiter for arguments

::

Namespace

Item::onCollision()

This definition of the onCollision() function is in the Item namespace

.

Field/Method selection

%obj.field

%obj.method()

Select a console method or field

//

Single-line comment

// This is a comment

Used to comment out a single line of code

/* */

Multi-line comment

/*This is a a

multi-line

comment*/

Used to comment out multiple consecutive lines

/* opens the comment, and */ closes it

There is no “comma operator”, as defined in C/C++; $a = 1, $b = 2; is a parse error.

Quote

Quote