Scripting: Operators – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "lang="cpp">" to "lang="C#">") |
Lou Montana (talk | contribs) m (Remove infobox about precedence since the precedence table has been moved at the top) |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{TOC|side}} | {{TOC|side}} | ||
'''Operators''' are elements used to generate operations between two values. | '''Operators''' are elements used to generate operations between two values. | ||
{{ | |||
{{TOC|subtoc|content= | |||
* 1 {{Link|#Precedence}} | |||
* 2 {{Link|#Types}} | |||
** 2.1 {{Link|#Assignment}} | |||
** 2.2 {{Link|#Arithmetic}} | |||
** 2.3 {{Link|#Relational}} | |||
** 2.4 {{Link|#Logical}} | |||
** 2.5 {{Link|#Bitwise}} | |||
** 2.6 {{Link|#String}} | |||
** 2.7 {{Link|#Indexing}} | |||
}} | |||
== Precedence == | |||
Operators precedence (one having execution priority over another) in Enforce Script is identical to the C language: | |||
<spoiler text="Show Enfusion Operator Precedence table"> | |||
{| class="wikitable" | |||
! Precedence | |||
! Operator | |||
! Description | |||
! Associativity | |||
|- | |||
! rowspan="5" | 1 | |||
| <enforce>++</enforce> | |||
| Postfix increment | |||
| rowspan="5" | Left-to-right | |||
|- | |||
| <enforce>--</enforce> | |||
| Postfix decrement | |||
|- | |||
| <enforce>()</enforce> | |||
| Function call | |||
|- | |||
| <enforce>[]</enforce> | |||
| Array subscripting | |||
|- | |||
| <enforce>.</enforce> | |||
| Element selection by reference | |||
|- | |||
! rowspan="8" | 2 | |||
| <enforce>++</enforce> | |||
| Prefix increment | |||
| rowspan="8" | Right-to-left | |||
|- | |||
| <enforce>--</enforce> | |||
| Prefix decrement | |||
|- | |||
| <enforce>+</enforce> | |||
| Unary plus | |||
|- | |||
| <enforce>-</enforce> | |||
| Unary minus | |||
|- | |||
| <enforce>!</enforce> | |||
| Logical NOT | |||
|- | |||
| <enforce>~</enforce> | |||
| Bitwise NOT | |||
|- | |||
| <enforce>new</enforce> | |||
| Dynamic memory allocation | |||
|- | |||
| <enforce>delete</enforce> | |||
| Dynamic memory deallocation | |||
|- | |||
! rowspan="3" | 3 | |||
| <enforce>*</enforce> | |||
| Multiplication | |||
| rowspan="3" | Left-to-right | |||
|- | |||
| <enforce>/</enforce> | |||
| Division | |||
|- | |||
| <enforce>%</enforce> | |||
| {{Link|https://en.wikipedia.org/wiki/Modulo_operation|Modulo}} (remainder) | |||
|- | |||
! rowspan="2" | 4 | |||
| <enforce>+</enforce> | |||
| Addition | |||
| rowspan="2" | Left-to-right | |||
|- | |||
| <enforce>-</enforce> | |||
| Subtraction | |||
|- | |||
! rowspan="2" | 5 | |||
| <enforce><<</enforce> | |||
| {{Link|https://en.wikipedia.org/wiki/Bitwise_operation|Bitwise}} left shift | |||
| rowspan="2" | Left-to-right | |||
|- | |||
| <enforce>>></enforce> | |||
| {{Link|https://en.wikipedia.org/wiki/Bitwise_operation|Bitwise}} right shift | |||
|- | |||
! rowspan="4" | 6 | |||
| <enforce><</enforce> | |||
| Less than | |||
| rowspan="4" | Left-to-right | |||
|- | |||
| <enforce><=</enforce> | |||
| Less than or equal to | |||
|- | |||
| <enforce>></enforce> | |||
| Greater than | |||
|- | |||
| <enforce>>=</enforce> | |||
| Greater than or equal to | |||
|- | |||
! rowspan="2" | 7 | |||
| <enforce>==</enforce> | |||
| Equal to | |||
| rowspan="2" | Left-to-right | |||
|- | |||
| <enforce>!=</enforce> | |||
| Not equal to | |||
|- | |||
! 11 | |||
| <enforce>&</enforce> | |||
| Bitwise AND | |||
| Left-to-right | |||
|- | |||
! 12 | |||
| <enforce>^</enforce> | |||
| Bitwise XOR (e'''x'''clusive '''or''') | |||
| Left-to-right | |||
|- | |||
! 13 | |||
| <enforce>|</enforce> | |||
| Bitwise OR (inclusive or) | |||
| Left-to-right | |||
|- | |||
! 14 | |||
| <enforce>&&</enforce> | |||
| Logical AND | |||
| Left-to-right | |||
|- | |||
! 15 | |||
| <enforce>||</enforce> | |||
| Logical OR | |||
| Left-to-right | |||
|- | |||
! rowspan="11" | 8 | |||
| <enforce>=</enforce> | |||
| Direct assignment | |||
| rowspan="11" | Right-to-left | |||
|- | |||
| <enforce>+=</enforce> | |||
| Assignment by sum | |||
|- | |||
| <enforce>-=</enforce> | |||
| Assignment by difference | |||
|- | |||
| <enforce>*=</enforce> | |||
| Assignment by product | |||
|- | |||
| <enforce>/=</enforce> | |||
| Assignment by quotient | |||
|- | |||
| <enforce>%=</enforce> | |||
| Assignment by remainder | |||
|- | |||
| <enforce><<=</enforce> | |||
| Assignment by bitwise left shift | |||
|- | |||
| <enforce>>>=</enforce> | |||
| Assignment by bitwise right shift | |||
|- | |||
| <enforce>&=</enforce> | |||
| Assignment by bitwise AND | |||
|- | |||
| <enforce>^=</enforce> | |||
| Assignment by bitwise XOR | |||
|- | |||
| <enforce>|=</enforce> | |||
| Assignment by bitwise OR | |||
|} | |||
</spoiler> or see the {{Link|https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence|Wikipedia article section}}. | |||
Line 8: | Line 185: | ||
=== Assignment === | === Assignment === | ||
<div style="font-family: monospace"> | |||
==== {{=}} ==== | ==== {{=}} ==== | ||
</div> | |||
The "assign operator sets the righthand value to the lefthand variable. | The "assign operator sets the righthand value to the lefthand variable. | ||
< | <enforce> | ||
int five = 5; | int five = 5; | ||
</ | </enforce> | ||
{{Feature|important|This operator is '''NOT''' an equality check. For this, see {{ | {{Feature|important|This operator is '''NOT''' an equality check. For this, see {{Link|#{{=}}{{=}}}}.}} | ||
<div style="font-family: monospace"> | |||
==== ++ ==== | ==== ++ ==== | ||
The increment operator adds 1 to the provided numerical variable. It only applies to int and float values. | </div> | ||
< | The increment (or post-increment) operator adds 1 to the provided numerical variable. It only applies to int and float values. | ||
<enforce> | |||
int result = 5; | int result = 5; | ||
result++; // result is 6 | result++; // result is 6 | ||
</ | </enforce> | ||
This operator can be used ''before'' the value as well (it is then called ''pre-increment'' operator): | |||
<enforce> | |||
int result = 5; | |||
++result; // result is 6 | |||
</enforce> | |||
The difference is that the ''returned'' value is incremented too: <!-- let's not talk about how ++i ''may'' be faster than i++ here --> | |||
{| | |||
| <enforce> | |||
int result = 5; | |||
Print(result++); // outputs 5, result is 6 | |||
</enforce> | |||
| <enforce> | |||
int result = 5; | |||
Print(++result); // outputs 6, result is 6 | |||
</enforce> | |||
|} | |||
<div style="font-family: monospace"> | |||
==== -- ==== | ==== -- ==== | ||
The decrement operator removes 1 to the provided numerical variable. It only applies to int and float values. | </div> | ||
< | The decrement (or post-decrement) operator removes 1 to the provided numerical variable. It only applies to int and float values. | ||
<enforce> | |||
int result = 5; | int result = 5; | ||
result--; // result is 4 | result--; // result is 4 | ||
</ | </enforce> | ||
Same as {{Link|#++}}, this operator can be used ''before'' the value (then called ''pre-decrement'' operator): | |||
<enforce> | |||
int result = 5; | |||
--result; // result is 4 | |||
</enforce> | |||
{| | |||
| <enforce> | |||
int result = 5; | |||
Print(result--); // outputs 5, result is 4 | |||
</enforce> | |||
| <enforce> | |||
int result = 5; | |||
Print(--result); // outputs 4, result is 4 | |||
</enforce> | |||
|} | |||
<div style="font-family: monospace"> | |||
==== +{{=}} ==== | ==== +{{=}} ==== | ||
</div> | |||
The "add to" operator adds the righthand value to the lefthand variable. | The "add to" operator adds the righthand value to the lefthand variable. | ||
< | <enforce> | ||
int result = 5; | int result = 5; | ||
result += 3; // result is 8 | result += 3; // result is 8 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== -{{=}} ==== | ==== -{{=}} ==== | ||
</div> | |||
The "subtract from" operator subtract the righthand value to the lefthand variable. | The "subtract from" operator subtract the righthand value to the lefthand variable. | ||
< | <enforce> | ||
int result = 5; | int result = 5; | ||
result -= 3; // result is 2 | result -= 3; // result is 2 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== *{{=}} ==== | ==== *{{=}} ==== | ||
</div> | |||
The "multiply with" operator multiplies the lefthand variable with the righthand value. | The "multiply with" operator multiplies the lefthand variable with the righthand value. | ||
< | <enforce> | ||
int result = 5; | int result = 5; | ||
result *= 3; // result is 15 | result *= 3; // result is 15 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== /{{=}} ==== | ==== /{{=}} ==== | ||
</div> | |||
The "divide by" operator multiplies the lefthand variable with the righthand value. | The "divide by" operator multiplies the lefthand variable with the righthand value. | ||
< | <enforce> | ||
int result = 5; | int result = 5; | ||
result /= 3; // result is 1 (floored int value) | result /= 3; // result is 1 (floored int value) | ||
</ | </enforce> | ||
=== Arithmetic === | === Arithmetic === | ||
<div style="font-family: monospace"> | |||
==== + ==== | ==== + ==== | ||
</div> | |||
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. | ||
< | <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) | ||
3.25 + 1.75; // result is 5.0 (float) | 3.25 + 1.75; // result is 5.0 (float) | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== - ==== | ==== - ==== | ||
</div> | |||
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. | ||
< | <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) | ||
3.25 - 1.25; // result is 2.0 (float) | 3.25 - 1.25; // result is 2.0 (float) | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== * ==== | ==== * ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
5 * 3; | 5 * 3; // result is 15 | ||
5 * 2.5; // result is 12.5 (float) | 5 * 2.5; // result is 12.5 (float) | ||
1.5 * 3.25; // result is 4.875 (float) | 1.5 * 3.25; // result is 4.875 (float) | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== / ==== | ==== / ==== | ||
</div> | |||
The "divided by" operator divides the left argument by the right one.<br> | The "divided by" operator divides the left argument by the right one.<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. | ||
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. | ||
< | <enforce> | ||
5 / 3; | 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) | ||
3.5 / 0.5; // result is 7.0 (float) | 3.5 / 0.5; // result is 7.0 (float) | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== % ==== | ==== % ==== | ||
</div> | |||
Modulo is an operator that gives the remainder of a division.<br> | Modulo is an operator that gives the remainder of a division.<br> | ||
It only applies to integers. | It only applies to integers. | ||
{{Feature|informative|See the | {{Feature|informative|See the <enforce inline>Math.Repeat()</enforce> method for a float modulo approximation.}} | ||
< | <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 | ||
</ | </enforce> | ||
=== Relational === | === Relational === | ||
<div style="font-family: monospace"> | |||
==== > ==== | ==== > ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
10 > 11; // false | 10 > 11; // false | ||
10 > 10; // false | 10 > 10; // false | ||
10 > 9; // true | 10 > 9; // true | ||
10 > 5; | 10 > 5; // true | ||
10 > 5.5; // true | 10 > 5.5; // true | ||
10.5 > 5; // true | 10.5 > 5; // true | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== < ==== | ==== < ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
10 < 11; // true | 10 < 11; // true | ||
10 < 10; // false | 10 < 10; // false | ||
10 < 9; // false | 10 < 9; // false | ||
10 < 5; | 10 < 5; // false | ||
10 < 5.5; // false | 10 < 5.5; // false | ||
10.5 < 5; // false | 10.5 < 5; // false | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== >{{=}} ==== | ==== >{{=}} ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
10 >= 11; // false | 10 >= 11; // false | ||
10 >= 10; // true | 10 >= 10; // true | ||
10 >= 9; // true | 10 >= 9; // true | ||
10 >= 5; // true | 10 >= 5; // true | ||
10 >= 5.5; // true | 10 >= 5.5; // true | ||
10.5 >= 5; // true | 10.5 >= 5; // true | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== <{{=}} ==== | ==== <{{=}} ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
10 <= 11; // true | 10 <= 11; // true | ||
10 <= 10; // true | 10 <= 10; // true | ||
10 <= 9; // false | 10 <= 9; // false | ||
10 <= 5; // false | 10 <= 5; // false | ||
10 <= 5.5; // false | 10 <= 5.5; // false | ||
10.5 <= 5; // false | 10.5 <= 5; // false | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== {{=}}{{=}} ==== | ==== {{=}}{{=}} ==== | ||
</div> | |||
The "equals" operator returns '''true''' if the left argument is strictly equal to the right argument, false otherwise. | The "equals" operator returns '''true''' if the left argument is strictly equal to the right argument, false otherwise. | ||
Applies to: | Applies to: | ||
* | * bool | ||
* int | * int | ||
* float | * float | ||
Line 169: | Line 415: | ||
* array (reference) | * array (reference) | ||
* object (reference) | * object (reference) | ||
< | <enforce> | ||
true == true; // true | true == true; // true | ||
true == false; // false | true == false; // false | ||
Line 187: | Line 433: | ||
MyClass object2 = new MyClass(); | MyClass object2 = new MyClass(); | ||
object1 == object2; // false - same type, different instance | object1 == object2; // false - same type, different instance | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== !{{=}} ==== | ==== !{{=}} ==== | ||
</div> | |||
The "different" operator returns '''true''' if the left argument is different from the right argument, false otherwise. | The "different" operator returns '''true''' if the left argument is different from the right argument, false otherwise. | ||
Applies to: | Applies to: | ||
* | * bool | ||
* int | * int | ||
* float | * float | ||
Line 201: | Line 449: | ||
* array (reference) | * array (reference) | ||
* object (reference) | * object (reference) | ||
< | <enforce> | ||
10 != 11; // true | 10 != 11; // true | ||
10 != 10; // false | 10 != 10; // false | ||
Line 216: | Line 464: | ||
MyClass object2 = new MyClass(); | MyClass object2 = new MyClass(); | ||
object1 != object2; // true - different instance | object1 != object2; // true - different instance | ||
</ | </enforce> | ||
=== Logical === | === Logical === | ||
<div style="font-family: monospace"> | |||
==== && ==== | ==== && ==== | ||
</div> | |||
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. | ||
< | <enforce> | ||
bool a = false; | |||
bool b = false; | |||
bool result = a && b; // result is false | |||
bool a = true; | |||
bool b = false; | |||
bool result = a && b; // result is false | |||
bool a = true; | |||
bool b = true; | |||
bool result = a && b; // result is true | |||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== {{!}}{{!}} ==== | ==== {{!}}{{!}} ==== | ||
</div> | |||
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). | ||
< | <enforce> | ||
bool a = false; | |||
bool b = false; | |||
bool result = a || b; // result is false | |||
bool a = true; | |||
bool b = false; | |||
bool result = a || b; // result is true | |||
bool a = true; | |||
bool b = true; | |||
bool result = a || b; // result is true | |||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== ! ==== | ==== ! ==== | ||
The "not" logical operator inverts a | </div> | ||
The "not" logical operator inverts a bool value: {{hl|true}} becomes {{hl|false}} and {{hl|false}} becomes {{hl|true}}.<br> | |||
It can also be used on: | It can also be used on: | ||
* int or float (0 comparison) | * int or float (0 comparison) | ||
* 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) | ||
< | <enforce> | ||
bool a = true; | |||
bool result = !a; // result is false | |||
bool a = false; | |||
bool result = !a; // result is true | |||
bool isAlive = player.IsAlive(); | |||
bool isDead = !isAlive; | |||
int health = 0; | int health = 0; | ||
if (!health) // same as if (health == 0) | if (!health) // same as if (health == 0) - not recommended | ||
Print("dead"); | Print("dead"); | ||
float fHealth = 0.0; | float fHealth = 0.0; | ||
if (!fHealth) // same as if (fHealth == 0) | if (!fHealth) // same as if (fHealth == 0) - not recommended | ||
Print("dead"); | Print("dead"); | ||
string data = ""; | string data = ""; | ||
if (!data) // same as if (data.IsEmpty()) and if (data == "") | if (!data) // same as if (data.IsEmpty()) and if (data == "") - not recommended | ||
Print("empty string"); | Print("empty string"); | ||
MyClass instance = null; | MyClass instance = null; | ||
if (!instance) // same as if (instance == null) | if (!instance) // same as if (instance == null) | ||
Print("instance is null!"); | Print("instance is null!"); | ||
// note that if (value) to check an object is -not- null works too | // note that if (value) to check an object is -not- null works too | ||
if (instance) // same as if (instance != null) | if (instance) // same as if (instance != null) | ||
Print("instance is NOT null!"); | Print("instance is NOT null!"); | ||
</enforce> | |||
</ | |||
=== Bitwise === | === Bitwise === | ||
<div style="font-family: monospace"> | |||
==== & ==== | ==== & ==== | ||
</div> | |||
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. | ||
< | <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 | ||
int result = a & b; // 00001001 in binary | int result = a & b; // 00001001 in binary | ||
// result is 9 | // result is 9 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== {{!}} ==== | ==== {{!}} ==== | ||
</div> | |||
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. | ||
< | <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 | ||
int result = a | b; // 00001111 in binary | int result = a | b; // 00001111 in binary | ||
// result is 15 | // result is 15 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== ~ ==== | ==== ~ ==== | ||
</div> | |||
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. | ||
< | <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 | ||
// result is -8 | // result is -8 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== << ==== | ==== << ==== | ||
</div> | |||
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. | ||
< | <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 336: | Line 588: | ||
// result is 00000100 | // result is 00000100 | ||
// result is 4 | // result is 4 | ||
</ | </enforce> | ||
<div style="font-family: monospace"> | |||
==== >> ==== | ==== >> ==== | ||
</div> | |||
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. | ||
< | <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 347: | Line 601: | ||
// result is 00000001 | // result is 00000001 | ||
// result is 1 | // result is 1 | ||
</ | </enforce> | ||
=== String === | === String === | ||
<div style="font-family: monospace"> | |||
==== + ==== | ==== + ==== | ||
</div> | |||
The "adds" operator used with strings concatenates them. | 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. | 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. | ||
< | <enforce> | ||
string a = "Hello"; | string a = "Hello"; | ||
string b = "there"; | string b = "there"; | ||
Line 361: | Line 617: | ||
string result = a + " " + b; // result is "Hello there" | string result = a + " " + b; // result is "Hello there" | ||
string | string stringifiedBool = "string " + true; // result is "string true" | ||
string stringifiedInt = "string " + 42; // result is "string 42" | string stringifiedInt = "string " + 42; // result is "string 42" | ||
string stringifiedFloat = "string " + 4.0; // result is "string 4" | string stringifiedFloat = "string " + 4.0; // result is "string 4" | ||
Line 374: | Line 630: | ||
string ok2 = "test" + 3 + true + false + 4; // will return "test3104" | string ok2 = "test" + 3 + true + false + 4; // will return "test3104" | ||
string error = 3 + "test"; // will throw a parsing error | string error = 3 + "test"; // will throw a parsing error | ||
</ | </enforce> | ||
=== Indexing === | === Indexing === | ||
<div style="font-family: monospace"> | |||
==== <nowiki>[]</nowiki> ==== | ==== <nowiki>[]</nowiki> ==== | ||
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). | </div> | ||
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 is '''very''' efficient on '''static''' arrays. | |||
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. | ||
< | <enforce> | ||
int staticNumbers[5] = { 1, 2, 3, 4, 5 }; | |||
int thirdStaticNumber = staticNumbers[2]; // result is 3, lightning fast on static arrays | |||
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 | ||
array<string> letters = { "A", "B", "C", "D", "E" }; | array<string> letters = { "A", "B", "C", "D", "E" }; | ||
string thirdLetter = letters[2]; // result is "C" | string thirdLetter = letters[2]; // result is "C" | ||
string text = "ABCDE"; | string text = "ABCDE"; | ||
string thirdLetter = text[2]; // result is "C" | string thirdLetter = text[2]; // result is "C" | ||
</ | </enforce> | ||
{{Feature|important| | |||
The {{hl|[i]}} operator is equivalent to a <enforce inline>.Get(i)</enforce> method call for anything else than static arrays; | |||
in order to save unrequired method calls, either use {{Link|Arma Reforger:Scripting: Keywords#foreach|foreach}} or cache the call result whenever possible. | |||
<enforce> | |||
// bad | |||
for (int i, count = myArray.Count(); i < count; i++) | |||
{ | |||
if (myArray[i].IsEmpty()) // .Get(i) equivalent | |||
Print("#" + i + " is empty"); | |||
else | |||
Print("trimmed '" + myArray[i] + "' is '" + myArray[i].Trim() + "'"); // .Get(i) equivalent, twice | |||
} | |||
== | // good | ||
for (int i, count = myArray.Count(); i < count; i++) | |||
{ | |||
string value = myArray[i]; // .Get(i) equivalent | |||
if (value.IsEmpty()) | |||
Print("#" + i + " is empty"); | |||
else | |||
Print("trimmed '" + value + "' is '" + value.Trim() + "'"); | |||
} | |||
// best | |||
foreach (int i, string value : myArray) | |||
{ | |||
if (value.IsEmpty()) | |||
Print("#" + i + " is empty"); | |||
else | |||
Print("trimmed '" + value + "' is '" + value.Trim() + "'"); | |||
} | |||
</enforce> | |||
}} | |||
{{GameCategory|armaR|Modding|Guidelines|Scripting}} | {{GameCategory|armaR|Modding|Guidelines|Scripting}} |
Latest revision as of 11:04, 17 May 2024
Operators are elements used to generate operations between two values.
- 1 Precedence
- 2 Types
- 2.1 Assignment
- 2.2 Arithmetic
- 2.3 Relational
- 2.4 Logical
- 2.5 Bitwise
- 2.6 String
- 2.7 Indexing
Precedence
Operators precedence (one having execution priority over another) in Enforce Script is identical to the C language:
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | ++ |
Postfix increment | Left-to-right |
-- |
Postfix decrement | ||
() |
Function call | ||
[] |
Array subscripting | ||
. |
Element selection by reference | ||
2 | ++ |
Prefix increment | Right-to-left |
-- |
Prefix decrement | ||
+ |
Unary plus | ||
- |
Unary minus | ||
! |
Logical NOT | ||
~ |
Bitwise NOT | ||
new |
Dynamic memory allocation | ||
delete |
Dynamic memory deallocation | ||
3 | * |
Multiplication | Left-to-right |
/ |
Division | ||
% |
Modulo (remainder) | ||
4 | + |
Addition | Left-to-right |
- |
Subtraction | ||
5 | << |
Bitwise left shift | Left-to-right |
>> |
Bitwise right shift | ||
6 | < |
Less than | Left-to-right |
<= |
Less than or equal to | ||
> |
Greater than | ||
>= |
Greater than or equal to | ||
7 | == |
Equal to | Left-to-right |
!= |
Not equal to | ||
11 | & |
Bitwise AND | Left-to-right |
12 | ^ |
Bitwise XOR (exclusive or) | Left-to-right |
13 | | |
Bitwise OR (inclusive or) | Left-to-right |
14 | && |
Logical AND | Left-to-right |
15 | || |
Logical OR | Left-to-right |
8 | = |
Direct assignment | Right-to-left |
+= |
Assignment by sum | ||
-= |
Assignment by difference | ||
*= |
Assignment by product | ||
/= |
Assignment by quotient | ||
%= |
Assignment by remainder | ||
<<= |
Assignment by bitwise left shift | ||
>>= |
Assignment by bitwise right shift | ||
&= |
Assignment by bitwise AND | ||
^= |
Assignment by bitwise XOR | ||
|= |
Assignment by bitwise OR |
or see the Wikipedia article section.
Types
Assignment
=
The "assign operator sets the righthand value to the lefthand variable.
++
The increment (or post-increment) operator adds 1 to the provided numerical variable. It only applies to int and float values.
This operator can be used before the value as well (it is then called pre-increment operator):
The difference is that the returned value is incremented too:
--
The decrement (or post-decrement) operator removes 1 to the provided numerical variable. It only applies to int and float values.
Same as ++, this operator can be used before the value (then called pre-decrement operator):
+=
The "add to" operator adds the righthand value to the lefthand variable.
-=
The "subtract from" operator subtract the righthand value to the lefthand variable.
*=
The "multiply with" operator multiplies the lefthand variable with the righthand value.
/=
The "divide by" operator multiplies the lefthand variable with the righthand value.
Arithmetic
+
he "adds" operator does an addition between two numeric values, float and/or integer.
-
The "subtract from" operator does a subtraction between two numerical values, float and/or integer.
*
The multiplication sign does a multiplication between two numerical values, float and/or integer.
/
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.
%
Modulo is an operator that gives the remainder of a division.
It only applies to integers.
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.
<
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.
>=
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.
<=
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.
==
The "equals" operator returns true if the left argument is strictly equal to the right argument, false otherwise.
Applies to:
- bool
- int
- float
- int / float, float / int
- string (case-sensitive)
- vector (value)
- array (reference)
- object (reference)
!=
The "different" operator returns true if the left argument is different from the right argument, false otherwise.
Applies to:
- bool
- int
- float
- int / float, float / int
- string (case-sensitive)
- vector (value)
- array (reference)
- object (reference)
Logical
&&
The "and" operator returns true if both left and right conditions are true.
||
The "or" logical operator returns true if left or right condition is true (or both).
!
The "not" logical operator inverts a bool 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)
Bitwise
&
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 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 NOT" operator reverts all bits of the provided value: 1 becomes 0 and vice-versa.
<<
The left shift operator moves all bits of the provided value to the left.
>>
The right shift operator moves bits of the provided value to the right.
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.
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 is very efficient on static arrays. It can also be used on strings, as strings are arrays of characters.