14th July 2010

# Operators

In Fallout 3's scripting language, several operators are available for the manipulation of data. This tutorial explains the function of these operators.

The operators available in Fallout 3's scripting language can be separated into two types, based on their syntax:

## Unary Operators

Unary operators are operators which work on a single operand, which comes after the operator itself. There is only one unary operator available in Fallout 3 scripting:

Name Operator Example Output
Negative - -1 -1

This unary operator is the only operator that can be used in function parameters, although not all functions will work correctly with negative parameters. It can be used on integers and floating point values only.

The GECK will not give a warning if you attempt to use "+" as a unary operator, but doing so will cause the script to halt. There is never a reason to use it in such a way, however, so you are unlikely to encounter this issue.

## Binary Operators

Binary operators are operators which work with two operands - one which comes before the operator and another which comes after. Two important properties that should be considered when working with binary operators are commutativity and associativity.

If a binary operator is commutative, then changing the order of a set of operations does not change the result. Using the addition operator as an example of a commutative operator, this means that:

`(A + B) + C = A + (B + C)`

If a binary operator is associative, then changing the order of operands does not change the result. Once again using the addition operator as an example of a commutative operator, this means that:

`A + B = B + A`

The binary operators available in Fallout 3 scripting can be sorted into three categories:

### Arithmetic Operators

Arithmetic operators are the only binary operators that can return values other than 0 and 1. There are five arithmetic operators available in Fallout 3 scripting, and you're probably already familiar with most of them.

Name Operator Commutative Associative Example Output
Addition + Yes Yes 3 + 4 7
Subtraction - No No 5 - 4 1
Multiplication * Yes Yes 6 * 2 12
Division / No No 12 / 4 3
Modulo % No No 10 % 3 1

The modulo operator returns the remainder of the integer division of the first value by the second. In the example above, 10 / 3 = 3 with remainder 1, so 10 % 3 returns 1.

All arithmetic operators, with the exception of modulo, can be used on integers and floating point values. The modulo operator can only be used on integers

There is no integer division operator in Fallout 3 scripting, but if a floating point value is assigned to an integer variable, it will be truncated.

### Comparison Operators

Comparison operators are used, as the name suggests, to compare two values. They can only return values of 1 or 0. You're probably already familiar with most of these, and if you've worked with a programming or scripting language before then you won't see anything new here. They're all very easy to understand anyway:

Name Operator Commutative Associative Example Output
Equal to == No Yes 1 == 1 1
Not equal to != No Yes 3 != 1 1
Greater than > No No 5 > 7 0
Less than < No No 5 < 7 1
Greater than or equal to >= No No 5 >= 7 0
Less than or equal to <= No No 5 <= 7 1

All comparison operators can be used on integers and floating point values. The "equal to" and "not equal to" operators can also be used with references.

All integers are treated internally as floating point values, so the integer "1" and the floating point value "1.0" are treated as one and the same (pun intended).

There are a few relationships between comparison operators that it can be worth being aware of:

`A == B + A != B = 1`
`A > B = B <= A`
`A < B = B >= A`

## Logical Operators

Logical operators, like comparison operators, will only ever return values of 0 or 1. While most programming and scripting languages have quite a few logical operators, Fallout 3's scripting language only includes two:

Name Operator Commutative Associative Example Output
And && Yes Yes 0 && 5 0
Or || Yes Yes 0 || 5 1

Both logical operators can be called on integers, floating point values, and references.

The "and" operator checks if both values are non-zero. If both values are non-zero, then the "and" operator will return 1, whereas if either value is 0, then the "and" operator will return 0.

The "or" operator checks if either value is non-zero. If both values are 0, then the "or" operator will return 0, whereas if either value is non-zero, then the "or" operator will return 1.

## Hierarchy

When creating more complex expressions that use many operators, it is important to be aware of the order in which the operators in your expression will evaluate. For example, take a look at this condition:

`3 * 1 == 1`

If you don't know which operator will be evaluated first, then it is impossible to predict how the expression will be resolved. While it will always be resolved in the same way, it is still best to include brackets in more complex conditions so that they're easy to follow:

`(3 * 1) == 1`

It is now much easier to see the result of this expression will be 0, whereas before it was impossible to tell whether the result would be 0 or 3 unless you're familiar with the hierarchy of these operators.

This following list shows the priority of operators in Fallout 3 scripts. Operators with higher priority will be resolved first, with 1 being the highest and 6 the lowest.

Name Operator Priority
Negative - 1
Multiplication * 2
Division / 2
Modulo % 2