Scripting: Operators – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "lang="cpp">" to "lang="C#">")
m (Text replacement - "<syntaxhighlight lang="C#">" to "<enforce>")
Line 10: Line 10:
==== {{=}} ====
==== {{=}} ====
The "assign operator sets the righthand value to the lefthand variable.
The "assign operator sets the righthand value to the lefthand variable.
<syntaxhighlight lang="C#">
<enforce>
int five = 5;
int five = 5;
</syntaxhighlight>
</syntaxhighlight>
Line 17: Line 17:
==== ++ ====
==== ++ ====
The increment operator adds 1 to the provided numerical variable. It only applies to int and float values.
The increment operator adds 1 to the provided numerical variable. It only applies to int and float values.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result++; // result is 6
result++; // result is 6
Line 24: Line 24:
==== -- ====
==== -- ====
The decrement operator removes 1 to the provided numerical variable. It only applies to int and float values.
The decrement operator removes 1 to the provided numerical variable. It only applies to int and float values.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result--; // result is 4
result--; // result is 4
Line 31: Line 31:
==== +{{=}} ====
==== +{{=}} ====
The "add to" operator adds the righthand value to the lefthand variable.
The "add to" operator adds the righthand value to the lefthand variable.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result += 3; // result is 8
result += 3; // result is 8
Line 38: Line 38:
==== -{{=}} ====
==== -{{=}} ====
The "subtract from" operator subtract the righthand value to the lefthand variable.
The "subtract from" operator subtract the righthand value to the lefthand variable.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result -= 3; // result is 2
result -= 3; // result is 2
Line 45: Line 45:
==== *{{=}} ====
==== *{{=}} ====
The "multiply with" operator multiplies the lefthand variable with the righthand value.
The "multiply with" operator multiplies the lefthand variable with the righthand value.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result *= 3; // result is 15
result *= 3; // result is 15
Line 52: Line 52:
==== /{{=}} ====
==== /{{=}} ====
The "divide by" operator multiplies the lefthand variable with the righthand value.
The "divide by" operator multiplies the lefthand variable with the righthand value.
<syntaxhighlight lang="C#">
<enforce>
int result = 5;
int result = 5;
result /= 3; // result is 1 (floored int value)
result /= 3; // result is 1 (floored int value)
Line 61: Line 61:
==== + ====
==== + ====
he "adds" operator does an addition between two numeric values, float and/or integer.
he "adds" operator does an addition between two numeric values, float and/or integer.
<syntaxhighlight lang="C#">
<enforce>
5 + 3; // result is 8 (int)
5 + 3; // result is 8 (int)
5 + 2.5; // result is 7.5 (float)
5 + 2.5; // result is 7.5 (float)
Line 69: Line 69:
==== - ====
==== - ====
The "subtract from" operator does a subtraction between two numerical values, float and/or integer.
The "subtract from" operator does a subtraction between two numerical values, float and/or integer.
<syntaxhighlight lang="C#">
<enforce>
5 - 3; // result is 2 (int)
5 - 3; // result is 2 (int)
5 - 2.5; // result is 2.5 (float)
5 - 2.5; // result is 2.5 (float)
Line 77: Line 77:
==== * ====
==== * ====
The multiplication sign does a multiplication between two numerical values, float and/or integer.
The multiplication sign does a multiplication between two numerical values, float and/or integer.
<syntaxhighlight lang="C#">
<enforce>
5 * 3; // result is 15
5 * 3; // result is 15
5 * 2.5; // result is 12.5 (float)
5 * 2.5; // result is 12.5 (float)
Line 87: Line 87:
It can be applied to int, float or a mix of those.
It can be applied to int, float or a mix of those.
If both values are int, the return value will be a '''floored''' integer as well.
If both values are int, the return value will be a '''floored''' integer as well.
<syntaxhighlight lang="C#">
<enforce>
5 / 3; // result is 1 (int): 5/3 = 1.666(...), but integer floors the value
5 / 3; // result is 1 (int): 5/3 = 1.666(...), but integer floors the value
5 / 2.5; // result is 2.0 (float)
5 / 2.5; // result is 2.0 (float)
Line 97: Line 97:
It only applies to integers.
It only applies to integers.
{{Feature|informative|See the [enfusion://ScriptEditor/scripts/Game/Global/Functions.c{{!}}SCR_Global].fmod}} method for a float modulo approximation.}}
{{Feature|informative|See the [enfusion://ScriptEditor/scripts/Game/Global/Functions.c{{!}}SCR_Global].fmod}} method for a float modulo approximation.}}
<syntaxhighlight lang="C#">
<enforce>
  5 % 3; // result is 2: 5 is one time 3, remaining 2
  5 % 3; // result is 2: 5 is one time 3, remaining 2
10 % 2; // result is 0: 10 is five times 2, remaining 0
10 % 2; // result is 0: 10 is five times 2, remaining 0
Line 108: Line 108:
The "greater than" operator returns '''true''' if the left argument is strictly greater than the right argument, false otherwise.<br>
The "greater than" operator returns '''true''' if the left argument is strictly greater than the right argument, false otherwise.<br>
It can be applied to int, float or a mix of those.
It can be applied to int, float or a mix of those.
<syntaxhighlight lang="C#">
<enforce>
10 > 11; // false
10 > 11; // false
10 > 10; // false
10 > 10; // false
Line 121: Line 121:
The "lesser than" operator returns '''true''' if the left argument is strictly smaller than the right argument, false otherwise.<br>
The "lesser than" operator returns '''true''' if the left argument is strictly smaller than the right argument, false otherwise.<br>
It can be applied to int, float or a mix of those.
It can be applied to int, float or a mix of those.
<syntaxhighlight lang="C#">
<enforce>
10 < 11; // true
10 < 11; // true
10 < 10; // false
10 < 10; // false
Line 134: Line 134:
The "greater than or equal" operator returns '''true''' if the left argument is greater than or equal to the right argument, false otherwise.<br>
The "greater than or equal" operator returns '''true''' if the left argument is greater than or equal to the right argument, false otherwise.<br>
It can be applied to int, float or a mix of those.
It can be applied to int, float or a mix of those.
<syntaxhighlight lang="C#">
<enforce>
10 >= 11; // false
10 >= 11; // false
10 >= 10; // true
10 >= 10; // true
Line 147: Line 147:
The "lesser than or equal" operator returns '''true''' if the left argument is smaller than or equal to the right argument, false otherwise.<br>
The "lesser than or equal" operator returns '''true''' if the left argument is smaller than or equal to the right argument, false otherwise.<br>
It can be applied to int, float or a mix of those.
It can be applied to int, float or a mix of those.
<syntaxhighlight lang="C#">
<enforce>
10 <= 11; // true
10 <= 11; // true
10 <= 10; // true
10 <= 10; // true
Line 169: Line 169:
* array (reference)
* array (reference)
* object (reference)
* object (reference)
<syntaxhighlight lang="C#">
<enforce>
true == true; // true
true == true; // true
true == false; // false
true == false; // false
Line 201: Line 201:
* array (reference)
* array (reference)
* object (reference)
* object (reference)
<syntaxhighlight lang="C#">
<enforce>
10 != 11; // true
10 != 11; // true
10 != 10; // false
10 != 10; // false
Line 222: Line 222:
==== &amp;&amp; ====
==== &amp;&amp; ====
The "and" operator returns '''true''' if both left and right conditions are true.
The "and" operator returns '''true''' if both left and right conditions are true.
<syntaxhighlight lang="C#">
<enforce>
boolean a = false;
boolean a = false;
boolean b = false;
boolean b = false;
Line 238: Line 238:
==== {{!}}{{!}} ====
==== {{!}}{{!}} ====
The "or" logical operator returns '''true''' if left or right condition is true (or both).
The "or" logical operator returns '''true''' if left or right condition is true (or both).
<syntaxhighlight lang="C#">
<enforce>
boolean a = false;
boolean a = false;
boolean b = false;
boolean b = false;
Line 258: Line 258:
* string (empty string comparison - usage of the {{hl|string.IsEmpty()}} method is preferred)
* string (empty string comparison - usage of the {{hl|string.IsEmpty()}} method is preferred)
* object (null comparison)
* object (null comparison)
<syntaxhighlight lang="C#">
<enforce>
boolean a = true;
boolean a = true;
boolean result = !a; // result is false
boolean result = !a; // result is false
Line 303: Line 303:
==== &amp; ====
==== &amp; ====
The "bitwise AND" operator applies a AND operation between values bits - both bits must be 1 to return 1, otherwise 0 is returned.
The "bitwise AND" operator applies a AND operation between values bits - both bits must be 1 to return 1, otherwise 0 is returned.
<syntaxhighlight lang="C#">
<enforce>
int a = 13; // 00001101 in binary
int a = 13; // 00001101 in binary
int b = 11; // 00001011 in binary
int b = 11; // 00001011 in binary
Line 312: Line 312:
==== {{!}} ====
==== {{!}} ====
The "bitwise OR" operator applies a OR operation between values bits - one of the bits must be 1 to return 1, otherwise 0 is returned.
The "bitwise OR" operator applies a OR operation between values bits - one of the bits must be 1 to return 1, otherwise 0 is returned.
<syntaxhighlight lang="C#">
<enforce>
int a = 13; // 00001101 in binary
int a = 13; // 00001101 in binary
int b = 11; // 00001011 in binary
int b = 11; // 00001011 in binary
Line 321: Line 321:
==== ~ ====
==== ~ ====
The "bitwise NOT" operator reverts all bits of the provided value: 1 becomes 0 and vice-versa.
The "bitwise NOT" operator reverts all bits of the provided value: 1 becomes 0 and vice-versa.
<syntaxhighlight lang="C#">
<enforce>
int a = 7; // 0000 0000 0000 0000 0000 0000 0000 0111 in binary (integer is 32 bits)
int a = 7; // 0000 0000 0000 0000 0000 0000 0000 0111 in binary (integer is 32 bits)
int result = ~a; // 1111 1111 1111 1111 1111 1111 1111 1000 in binary
int result = ~a; // 1111 1111 1111 1111 1111 1111 1111 1000 in binary
Line 329: Line 329:
==== &lt;&lt; ====
==== &lt;&lt; ====
The left shift operator moves all bits of the provided value to the left.
The left shift operator moves all bits of the provided value to the left.
<syntaxhighlight lang="C#">
<enforce>
int a = 1; // 00000001 in binary
int a = 1; // 00000001 in binary
int result = a << 2; // shift 'a' bits 2 steps to the left
int result = a << 2; // shift 'a' bits 2 steps to the left
Line 340: Line 340:
==== &gt;&gt; ====
==== &gt;&gt; ====
The right shift operator moves bits of the provided value to the right.
The right shift operator moves bits of the provided value to the right.
<syntaxhighlight lang="C#">
<enforce>
int a = 6; // 00000110 in binary
int a = 6; // 00000110 in binary
int result = a >> 2; // shift 'a' bits 2 steps to the left
int result = a >> 2; // shift 'a' bits 2 steps to the left
Line 355: Line 355:
When it is used with string and another type, it will stringify (calling its .ToString() method) the other value.
When it is used with string and another type, it will stringify (calling its .ToString() method) the other value.
The left argument '''must''' be of string type for it to happen.
The left argument '''must''' be of string type for it to happen.
<syntaxhighlight lang="C#">
<enforce>
string a = "Hello";
string a = "Hello";
string b = "there";
string b = "there";
Line 381: Line 381:
The "get index" operator is used on arrays to get an element by its '''zero-based''' index (e.g 0 is first element, 1 is second, etc).
The "get index" operator is used on arrays to get an element by its '''zero-based''' index (e.g 0 is first element, 1 is second, etc).
It can also be used on strings, as strings are arrays of characters.
It can also be used on strings, as strings are arrays of characters.
<syntaxhighlight lang="C#">
<enforce>
array<int> numbers = { 1, 2, 3, 4, 5 };
array<int> numbers = { 1, 2, 3, 4, 5 };
int thirdNumber = letters[2]; // result is 3
int thirdNumber = letters[2]; // result is 3

Revision as of 20:17, 30 July 2022

Operators are elements used to generate operations between two values.

Operators have a precedence order; see Precedence section below for more information.


Types

Assignment

=

The "assign operator sets the righthand value to the lefthand variable. <enforce> int five = 5; </syntaxhighlight>

This operator is NOT an equality check. For this, see ==.

++

The increment operator adds 1 to the provided numerical variable. It only applies to int and float values. <enforce> int result = 5; result++; // result is 6 </syntaxhighlight>

--

The decrement operator removes 1 to the provided numerical variable. It only applies to int and float values. <enforce> int result = 5; result--; // result is 4 </syntaxhighlight>

+=

The "add to" operator adds the righthand value to the lefthand variable. <enforce> int result = 5; result += 3; // result is 8 </syntaxhighlight>

-=

The "subtract from" operator subtract the righthand value to the lefthand variable. <enforce> int result = 5; result -= 3; // result is 2 </syntaxhighlight>

*=

The "multiply with" operator multiplies the lefthand variable with the righthand value. <enforce> int result = 5; result *= 3; // result is 15 </syntaxhighlight>

/=

The "divide by" operator multiplies the lefthand variable with the righthand value. <enforce> int result = 5; result /= 3; // result is 1 (floored int value) </syntaxhighlight>

Arithmetic

+

he "adds" operator does an addition between two numeric values, float and/or integer. <enforce> 5 + 3; // result is 8 (int) 5 + 2.5; // result is 7.5 (float) 3.25 + 1.75; // result is 5.0 (float) </syntaxhighlight>

-

The "subtract from" operator does a subtraction between two numerical values, float and/or integer. <enforce> 5 - 3; // result is 2 (int) 5 - 2.5; // result is 2.5 (float) 3.25 - 1.25; // result is 2.0 (float) </syntaxhighlight>

*

The multiplication sign does a multiplication between two numerical values, float and/or integer. <enforce> 5 * 3; // result is 15 5 * 2.5; // result is 12.5 (float) 1.5 * 3.25; // result is 4.875 (float) </syntaxhighlight>

/

The "divided by" operator divides the left argument by the right one.
It can be applied to int, float or a mix of those. If both values are int, the return value will be a floored integer as well. <enforce> 5 / 3; // result is 1 (int): 5/3 = 1.666(...), but integer floors the value 5 / 2.5; // result is 2.0 (float) 3.5 / 0.5; // result is 7.0 (float) </syntaxhighlight>

%

Modulo is an operator that gives the remainder of a division.
It only applies to integers.

See the [1].fmod

method for a float modulo approximation.}}

<enforce>

5 % 3; // result is 2: 5 is one time 3, remaining 2

10 % 2; // result is 0: 10 is five times 2, remaining 0 </syntaxhighlight>


Relational

>

The "greater than" operator returns true if the left argument is strictly greater than the right argument, false otherwise.
It can be applied to int, float or a mix of those. <enforce> 10 > 11; // false 10 > 10; // false 10 > 9; // true

10 > 5; // true 10 > 5.5; // true 10.5 > 5; // true </syntaxhighlight>

<

The "lesser than" operator returns true if the left argument is strictly smaller than the right argument, false otherwise.
It can be applied to int, float or a mix of those. <enforce> 10 < 11; // true 10 < 10; // false 10 < 9; // false

10 < 5; // false 10 < 5.5; // false 10.5 < 5; // false </syntaxhighlight>

>=

The "greater than or equal" operator returns true if the left argument is greater than or equal to the right argument, false otherwise.
It can be applied to int, float or a mix of those. <enforce> 10 >= 11; // false 10 >= 10; // true 10 >= 9; // true

10 >= 5; // true 10 >= 5.5; // true 10.5 >= 5; // true </syntaxhighlight>

<=

The "lesser than or equal" operator returns true if the left argument is smaller than or equal to the right argument, false otherwise.
It can be applied to int, float or a mix of those. <enforce> 10 <= 11; // true 10 <= 10; // true 10 <= 9; // false

10 <= 5; // false 10 <= 5.5; // false 10.5 <= 5; // false </syntaxhighlight>

==

The "equals" operator returns true if the left argument is strictly equal to the right argument, false otherwise.

Applies to:

  • boolean
  • int
  • float
  • int / float, float / int
  • string (case-sensitive)
  • vector (value)
  • array (reference)
  • object (reference)

<enforce> true == true; // true true == false; // false

10 == 11; // false 10 == 10; // true 10 == 9; // false

10 == 10.0; // true 10 == 10.1; // false

MyClass object1 = new MyClass(); MyClass object2 = object1; object1 == object2; // true - same type, same instance

MyClass object1 = new MyClass(); MyClass object2 = new MyClass(); object1 == object2; // false - same type, different instance </syntaxhighlight>

!=

The "different" operator returns true if the left argument is different from the right argument, false otherwise.

Applies to:

  • boolean
  • int
  • float
  • int / float, float / int
  • string (case-sensitive)
  • vector (value)
  • array (reference)
  • object (reference)

<enforce> 10 != 11; // true 10 != 10; // false 10 != 9; // true

10 != 10.0; // false 10 != 10.1; // true

MyClass object1 = new MyClass(); MyClass object2 = object1; object1 != object2; // false - same instance

MyClass object1 = new MyClass(); MyClass object2 = new MyClass(); object1 != object2; // true - different instance </syntaxhighlight>

Logical

&&

The "and" operator returns true if both left and right conditions are true. <enforce> boolean a = false; boolean b = false; boolean result = a && b; // result is false

boolean a = true; boolean b = false; boolean result = a && b; // result is false

boolean a = true; boolean b = true; boolean result = a && b; // result is true </syntaxhighlight>

||

The "or" logical operator returns true if left or right condition is true (or both). <enforce> boolean a = false; boolean b = false; boolean result = a || b; // result is false

boolean a = true; boolean b = false; boolean result = a || b; // result is true

boolean a = true; boolean b = true; boolean result = a || b; // result is true </syntaxhighlight>

!

The "not" logical operator inverts a boolean value: true becomes false and false becomes true.
It can also be used on:

  • int or float (0 comparison)
  • string (empty string comparison - usage of the string.IsEmpty() method is preferred)
  • object (null comparison)

<enforce> boolean a = true; boolean result = !a; // result is false

boolean a = false; boolean result = !a; // result is true

boolean isAlive = player.IsAlive(); boolean isDead = !isAlive;

int health = 0; if (!health) // same as if (health == 0) { Print("dead"); }

float fHealth = 0.0; if (!fHealth) // same as if (fHealth == 0) { Print("dead"); }

string data = ""; if (!data) // same as if (data.IsEmpty()) and if (data == "") { Print("empty string"); }

MyClass instance = null; if (!instance) // same as if (instance == null) { Print("instance is null!"); }

// note that if (value) to check an object is -not- null works too if (instance) // same as if (instance != null) { Print("instance is NOT null!"); } </syntaxhighlight>

Bitwise

&

The "bitwise AND" operator applies a AND operation between values bits - both bits must be 1 to return 1, otherwise 0 is returned. <enforce> int a = 13; // 00001101 in binary int b = 11; // 00001011 in binary int result = a & b; // 00001001 in binary // result is 9 </syntaxhighlight>

|

The "bitwise OR" operator applies a OR operation between values bits - one of the bits must be 1 to return 1, otherwise 0 is returned. <enforce> int a = 13; // 00001101 in binary int b = 11; // 00001011 in binary int result = a | b; // 00001111 in binary // result is 15 </syntaxhighlight>

~

The "bitwise NOT" operator reverts all bits of the provided value: 1 becomes 0 and vice-versa. <enforce> int a = 7; // 0000 0000 0000 0000 0000 0000 0000 0111 in binary (integer is 32 bits) int result = ~a; // 1111 1111 1111 1111 1111 1111 1111 1000 in binary // result is -8 </syntaxhighlight>

<<

The left shift operator moves all bits of the provided value to the left. <enforce> int a = 1; // 00000001 in binary int result = a << 2; // shift 'a' bits 2 steps to the left // leftmost bits are pushed "out" // rightmost bits are filled with zeros // result is 00000100 // result is 4 </syntaxhighlight>

>>

The right shift operator moves bits of the provided value to the right. <enforce> int a = 6; // 00000110 in binary int result = a >> 2; // shift 'a' bits 2 steps to the left // leftmost bits are filled with zeros // rightmost bits are pushed "out" // result is 00000001 // result is 1 </syntaxhighlight>

String

+

The "adds" operator used with strings concatenates them. When it is used with string and another type, it will stringify (calling its .ToString() method) the other value. The left argument must be of string type for it to happen. <enforce> string a = "Hello"; string b = "there"; string result = a + b; // result is "Hellothere" string result = a + " " + b; // result is "Hello there"

string stringifiedBoolean = "string " + true; // result is "string true" string stringifiedInt = "string " + 42; // result is "string 42" string stringifiedFloat = "string " + 4.0; // result is "string 4" string stringifiedFloat = "string " + 4.2; // result is "string 4.2" string stringifiedArray = "string " + myArray; // result is e.g "string 0x000002820B75EA68 {0,1,2,3,4}" string stringifiedVector = "string " + myVector; // result is e.g "string <0.000000, 0.500000, 1.000000>" string stringifiedEnum = "string " + MyEnum.B; // result is e.g "string 1" string stringifiedObject = "string " + myObject; // result is e.g "string MyClass<0x00000282360ECFD0>" string stringifiedTypename = "string " + MyClass; // result is e.g "string MyClass"

string ok1 = "test" + 3; // will return "test3" string ok2 = "test" + 3 + true + false + 4; // will return "test3104" string error = 3 + "test"; // will throw a parsing error </syntaxhighlight>

Indexing

[]

The "get index" operator is used on arrays to get an element by its zero-based index (e.g 0 is first element, 1 is second, etc). It can also be used on strings, as strings are arrays of characters. <enforce> array<int> numbers = { 1, 2, 3, 4, 5 }; int thirdNumber = letters[2]; // result is 3

array<string> letters = { "A", "B", "C", "D", "E" }; string thirdLetter = letters[2]; // result is "C"

string text = "ABCDE"; string thirdLetter = text[2]; // result is "C" </syntaxhighlight>


Precedence

Operators precedence (one having execution priority over another) in Enforce Script is identical to the C language: see the Wikipedia article section.