Operators are the base commands each programming language is built on. They provide ability to perform basic mathematical and logical operations.
Requirements
To understand this article, you should read the following articles:
Terms
Operand An operand is any value given to an operator.
Expression An expression is basically any code that returns a value. Read expression for more information.
Unary Operator An unary operator is an operator that requires only one operand.
Unary operation:
*operator* expression
Binary Operator A binary operator is an operator that requires two operands.
Binary operation:
expression *operator* expression
Operators
Assignment Operators
Assignment operators are used to assign values to a variable. OFP's scripting language provides only one assignment operator.
Assignment: identifier = expression
Example 1:
a = b
You might think that this operator compares a and b, but that is not the case. = simply sets the left value to be the right one. Other assignment operators like , that can be found in other programming languages do not exist in SQF/SQS.
Example 2:
a = b * c
Arithmetic Operators
Remember arithmetic operations from school? These work just the same way. All operands of arithmetic operations must be Numbers. Arithmetic operations always return a Number.
| + Unary arithmetic operators (in order of precedence) | Operator | Name | Example |
| - | Negation | sqf |
|
| + | Duplication | sqf |
|
| ( | Bracket | sqf |
| + Binary arithmetic operators | Operator | Name | Example |
| + | Addition | sqf |
|
| - | Subtraction | sqf |
|
| * | Multiplication | sqf |
|
| / | Division | sqf |
|
| % | Modulo | sqf |
|
| mod | Modulo | sqf |
|
| ^ | Raise to the power of | sqf |
Note
See also Math Commands.
Logical Operators
Logical operators evaluate Boolean values. All operands of logical operations are Booleans. A logical operation always returns a Boolean.
| + Unary logical operators | Operator | Name | Example |
| ! | Not | sqf |
|
| not | Not | sqf |
The Not-operator always returns the inverse Boolean value. If a Boolean a is true, !a returns false and vice versa.
| + Binary logical operators | Operator | Name | Example |
| && | And | sqf |
|
| and | And | sqf |
|
| || | Or | sqf |
|
| or | Or | sqf |
Andonly returnstrueif both operands aretrueOrreturnstrueif one or both operands aretrue
There is no Xor, Nor and Nand operator. Those can be simulated using the basic operators though:
| + Combined logical operators | Name | Combination |
| Xor | sqf |
|
| Nor | sqf |
|
| Nand | sqf |
Xorreturnstrueif exactly one of both values istrueNorreturnstrueif none of both values istrueNandreturnstrueif not both values aretrueat the same time
Comparison Operators
Comparison operators compare two values. Operands of comparisons may be of type Number, Side, String, Object, Group, Structured Text, Config, Display or Control for and , and Number for .
Comparisons always return a Boolean: true if the comparison matches, false if not.
| + Comparison operators | Operator | Name | Example |
| == | Equal | sqf |
|
| != | Not equal | sqf |
|
| < | Less than | sqf |
|
| > | Greater than | sqf |
|
| <= | Less or equal | sqf |
|
| >= | Greater or equal | sqf |
Note
Array Operators
The scripting language offers own operators to deal with arrays. All operands, of course, have to be of type Array. The return value of an array operation is an Array.
| + Unary array operators | Operator | Name | Example |
| + | Copy | sqf |
Normally arrays are assigned by reference. That means, if you assign array a to array b and change a afterwards, also b is changed. Use the copy operator to avoid this otherwise useful feature.
Example 1:
_arrayA = [1,2];
_arrayB = _arrayA;
_arrayA set [0,5];
// _arrayA => [5,2]
// _arrayB => [5,2]
Example 2:
_arrayA = [1,2];
_arrayB = +_arrayA;
_arrayA set [0,5];
// _arrayA => [5,2]
// _arrayB => [1,2]
| + Binary array operators | Operator | Name | Example |
| + | Concatenation | sqf |
|
| - | Removal | sqf |
+adds the second operand on the end of the first operand-removes all elements of the second operand from the first operand
Example 1:
_arrayA = [1,2];
_arrayB = [3,2,4];
_arrayC = _arrayA + _arrayB;
// _arrayC => [1,2,3,2,4]
Example 2:
_arrayA = [1,2,3,2,4];
_arrayB = [2,3];
_arrayC = _arrayA - _arrayB;
// _arrayC => [1,4]
String Operators
The scripting language offers one single string operator to concatenate strings. Both operands must be Strings. The return value of a string operation is a String.
| + Binary string operators | Operator | Name | Example |
| + | Concatenation | sqf |
+adds the second operand on the end of the first operand
Example:
_stringA = "Hello ";
_stringB = "World!";
_stringC = _stringA + _stringB;
// _stringC => "Hello World!"
Config Operators
| + Config operators | Operator | Name | Example |
| += | Append Array | cpp |
Example:
class B_Soldier_F;
class MySoldier: B_Soldier_F
{
RespawnItems[] += {"FirstAidKit"}; //Adds additional FAK
};
/*
Original RespawnItems[] array in config viewer:
RespawnItems[] = {"FirstAidKit"};
Modified RespawnItems[] array in config viewer:
RespawnItems[] = {"FirstAidKit","FirstAidKit"};
*/
Order of Precedence
See Order of Precedence.