| Home Page | Recent Changes | Preferences

Operators

Basics

Preoperators / Postoperators

If any operator is available as pre- and postoperator both versions basically have the same effect on the variable. The only difference is what they return: Preoperators return the variable's new value (after the operator's effect) while postoperators return the old value (the value before the operator did something).

Precedence of operators

The precedence of operators is used to group expressions.

local int a,b,c,d,e;

a=2; b=3; c=4; d=2; e=5;

                    // log output:
log(a@b@c@d@e);     // 2 3 4 2 5
log(a*b+++c**d*e);  // 86.000000
log(a@b@c@d@e);     // 2 4 4 2 5

UnrealScript actually evaluates the expression like this:

 (a * (b++)) + ((c ** d) * e)

You should use a form that uses some spaces and only few (or no) parentheses. This helps understanding the expression a lot when you look at it again after some time:

 a * b++ + c**d * e

Boolean Operators

iff (if and only if)
The word "iff" means "if and only if," implying that if the given condition isn't met, the operation will yield a different (the opposite) result. (This is maths and logic jargon, not an UnrealScript operator.)
short-circuit
Some binary boolean operators only evaluate their right operand if the result of the entire expression isn't already defined by the result of the first operand. In this case, the right operand is skipped, which is a particularly handy feature if evaluation of that right operand has non-trivial side effects (like a function call) or relies on the left operand's result.
if (bAlreadyConfirmed || ShowMessageBox("Are you sure?")) ...  // show message only when needed
if (ThisPawn != None && ThisPawn.bIsPlayer) ...                // prevent "Accessed None"s
Operation Operator Precedence Description Short-circuit
Negation ! Unary Negates a boolean expression
And && 30 True iff both operands are true First operand false → expression false
Exclusive Or ^^ 30 True iff one of the operands is true (but not both)
Inclusive Or || 32 True iff one or both of the operands are true First operand true → expression true

See also:

Comparison Operators

Comparison operators apply to values of all types. Only compatible types can be compared (numbers with numbers, strings with strings and so on). Object (classes, actors, textures, etc.) and Name properties can only be compared using Equal or Unequal.

Operation Operator Precedence Remarks
Equal == 24
Approximately Equal ~= 24 Equality within 0.0001 (numbers), case-insensitivity (strings)
Less < 24
Less or Equal <= 24
Greater > 24
Greater or Equal >= 24
Not Equal != 26

Conditional Operator ??

The conditional operator ?? can be used as a shorthand for what would normally be single line if .. then .. else blocks. If used appropriately it can enhance the readability of code. The conditional operator must always be applied to an expression that will return a boolean result.

// The form of the conditional operator is:
boolean condition ?? value if condition is true : value if condition is false

// Some example code.    (boolean expression)      (result if true)     (result if false)
GRI.SuddenDeathSeconds = SuddenDeathSeconds > 0 ?? SuddenDeathSeconds : 120;

This operator is Devastation specific.

EntropicLqd: Be interesting to see if they added support for it within the engine in UT2004 (not that I'm planning on getting it).

Numeric Operators

Numeric operators apply to values of type int, float and byte.

Operation Operator Precedence Description
Negation - Unary Returns the negative value of the operand
Increment ++ Pre-/Postoperator Increments the variable operand by 1
Decrement -- Pre-/Postoperator Decrements the variable operand by 1
Exponentiation ** 12 Puts the first operand to the power of the second operand
Multiplication * 16 Multiplies both operands
Division / 16 Divides the first operand by the second operand
Modulo % 18 Divides the first operand by the second and returns the remainder
Addition + 20 Adds both operands
Subtraction - 20 Subtracts the second operand from the first operand

The modulo operator has a bug:

 -8 % 10 = -8

This should be 2, not -8. See Useful Maths Functions for a fix.

The assignment operator = can be combined with +, -, * and /. These combined operators +=, -=, *= and /= assign the result of the operation to the first operand (which must be a variable). These combined assignment operators also return the new value of the variable, so

b += c;      // equivalent to  b = b + c;
a = b += c;  // equivalent to  b = b + c;  a = b;  or  a = (b = b + c);

will add b and c and assign the result to a and b.

Bitwise Operators

Operation Operator Precedence Description
Inversion ~ Unary Performs a bitwise inversion of the operand
Shift Left << 22 Shifts the bits of the first operand to the left
Shift Right (Arithmetic) >> 22 Shifts the bits of the first operand right, maintaining its signum
Shift Right >>> 22 Shifts the bits of the first operand right, filling with zeroes
And & 28 Bitwise and
Or | 28 Bitwise or
Exclusive Or ^ 28 Bitwise exclusive or

There is a small bug in the UnrealScript shifting operators that coders should be wary of (well, all the ones that use bitwise operators... which isn't much). UnrealScript converts the signed number into an unsigned number by means of two's complement before it performs the operation. Mathematically, I don't know why the numbers should be different, but they are. – WheatPuppet

The steps UScript goes though to get the "right" answer:

 1111 1111 1111 1111 1111 1111 1000 0101  =  -123 //Our input
 -123 << -2  //The operation we want it to do
 0000 0000 0000 0000 0000 0000 0111 1011 = 123 //Two's Complement
 //Two's Complement is an operation computers use to change the sign of a number. Basically, invert the bits and add one
 123 << -2 //Our new, unwanted, operation
 1100 0000 0000 0000 0000 0000 0001 1110 = -1073741794 //The result of that operation
 0011 1111 1111 1111 1111 1111 1110 0010 = 1073741794 //Two's Complement, and not the right answer

Note that 123 << -2 works fine, the bits shift properly and the result is found. You can easily see this operation above by removing the first and last steps. This is misleading because your Windows Calculator will say otherwise. I calculated the bit values and, yes, it is correct. The most significant bit is the sign bit and if it is one, the output will be negiative.

The moral of the story is not to use the bitwise operators if you are dealing with signed numbers. It doesn't handle them well. Somebody should make a new operator that handles signed shifts.

Vector Operators

See also vector.

Operation Operator Precedence Description
Reverse - Unary Returns the negative value of the operand
Multiply components * 16 Multiplies the corresponding components of both vectors
Multiply by scalar * 16 (vector * float or float * vector)
Divide by scalar / 16 (vector / float)
Dot product Dot 16 Calculates the dot product (inner product) of the vectors
Cross product Cross 16 Calculates the cross product (outer product) of the vectors
Addition + 20 Adds both vectors
Subtraction - 20 Reverses the second vector and adds it to the first one

Here the assignment operator = can also be combined with +, -, * and /. All combined assignment operators have a precedence of 34, so be careful when combining them with the string operators $ and @ since those have a precedence of 40.

Rotator Operators

See also rotator.

Operation Operator Precedence Description
Multiplication * 16 Multiplies all components of the rotator
Division / 16 Divides all components
Addition + 20 Adds the rotations
Subtraction - 20 Reverses the second rotator and adds it to the first one
Rotate vector >> 20 Rotates the vector in the way described by the rotator
Rotate vector (reversed) << 20 Rotates the vector in the way described by the rotator, but in reversed direction
Check for clockwise rotation (?) (UT2003 only) ClockwiseFrom 24 Pass the same components from different rotators to this operator and it returns, whether the rotation was clockwise or not.

Again, the assignment operator = can be combined with +, -, * and /.

Note that rotators can't be inverted with the - preoperator. You will have to use -1 * theRotator for this operation.

String Operators

Operation Operator Precedence Description
String Concatenation @ 40 The two strings are put together with a space inbetween.
String Concatenation $ 40 The two strings are put together without any space inbetween.

Color Operators

Color operators are only available in subclasses of Actor.

Operation Operator Precedence Description
Multiplication * 16 Multiplies all components of the rotator
Addition + 20 Adds the rotations
Subtraction - 20 Reverses the second rotator and adds it to the first one

There are no combined assignment operators for colors.

Complete Table Of Precedences

The higher an operator is in this table the more tightly it binds.

Operator class Examples
Parentheses ( )
Unary operators - ! ~ ++ --
Exponentiation **
Multiplicative operators * / Cross Dot
Additive operators + -
Bit shifting and vector rotating operators << >> >>>
Comparison operators (except inequality) < > <= >= ~= == ClockwiseFrom
Inequality !=
Bitwise Integer operators & | ^
Logical AND, logical XOR && ^^
Logical OR ||
Combined assignment operators += -= *= /=
String concatenation @ $

Related Topics

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In