Scripting: Operators – Arma Reforger
Lou Montana (talk | contribs) (Add ++i & --i, Change h4 font, Add xxx[i] note & example) |
Lou Montana (talk | contribs) (Add static arrays specificity) |
||
Line 470: | Line 470: | ||
==== <nowiki>[]</nowiki> ==== | ==== <nowiki>[]</nowiki> ==== | ||
</div> | </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). | 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> | <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> | </enforce> | ||
{{Feature|important| | {{Feature|important| | ||
The {{hl|[i]}} operator is equivalent to a <enforce inline>.Get(i)</enforce> method call; in order to save unrequired method calls, either use {{Link|Arma Reforger:Scripting: Keywords#foreach|foreach}} or cache the call result whenever possible. | 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> | <enforce> | ||
for (int i, count = myArray.Count(); i < count; i++) | for (int i, count = myArray.Count(); i < count; i++) |
Revision as of 21:29, 16 June 2023
Operators are elements used to generate operations between two values.
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:
- boolean
- 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:
- boolean
- 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 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)
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.
Precedence
Operators precedence (one having execution priority over another) in Enforce Script is identical to the C language: see the Wikipedia article section.