Scripting: Operators – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<syntaxhighlight lang="C#">" to "<enforce>")
m (Text replacement - "</syntaxhighlight>" to "</enforce>")
Line 12: Line 12:
<enforce>
<enforce>
int five = 5;
int five = 5;
</syntaxhighlight>
</enforce>
{{Feature|important|This operator is '''NOT''' an equality check. For this, see {{HashLink|#{{=}}{{=}}}}.}}
{{Feature|important|This operator is '''NOT''' an equality check. For this, see {{HashLink|#{{=}}{{=}}}}.}}


Line 20: Line 20:
int result = 5;
int result = 5;
result++; // result is 6
result++; // result is 6
</syntaxhighlight>
</enforce>


==== -- ====
==== -- ====
Line 27: Line 27:
int result = 5;
int result = 5;
result--; // result is 4
result--; // result is 4
</syntaxhighlight>
</enforce>


==== +{{=}} ====
==== +{{=}} ====
Line 34: Line 34:
int result = 5;
int result = 5;
result += 3; // result is 8
result += 3; // result is 8
</syntaxhighlight>
</enforce>


==== -{{=}} ====
==== -{{=}} ====
Line 41: Line 41:
int result = 5;
int result = 5;
result -= 3; // result is 2
result -= 3; // result is 2
</syntaxhighlight>
</enforce>


==== *{{=}} ====
==== *{{=}} ====
Line 48: Line 48:
int result = 5;
int result = 5;
result *= 3; // result is 15
result *= 3; // result is 15
</syntaxhighlight>
</enforce>


==== /{{=}} ====
==== /{{=}} ====
Line 55: Line 55:
int result = 5;
int result = 5;
result /= 3; // result is 1 (floored int value)
result /= 3; // result is 1 (floored int value)
</syntaxhighlight>
</enforce>


=== Arithmetic ===
=== Arithmetic ===
Line 65: Line 65:
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)
</syntaxhighlight>
</enforce>


==== - ====
==== - ====
Line 73: Line 73:
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)
</syntaxhighlight>
</enforce>


==== * ====
==== * ====
Line 81: Line 81:
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)
</syntaxhighlight>
</enforce>


==== / ====
==== / ====
Line 91: Line 91:
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)
</syntaxhighlight>
</enforce>


==== % ====
==== % ====
Line 100: Line 100:
  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
</syntaxhighlight>
</enforce>




Line 116: Line 116:
10 > 5.5; // true
10 > 5.5; // true
10.5 > 5; // true
10.5 > 5; // true
</syntaxhighlight>
</enforce>


==== &lt; ====
==== &lt; ====
Line 129: Line 129:
10 < 5.5; // false
10 < 5.5; // false
10.5 < 5; // false
10.5 < 5; // false
</syntaxhighlight>
</enforce>


==== &gt;{{=}} ====
==== &gt;{{=}} ====
Line 142: Line 142:
10 >= 5.5; // true
10 >= 5.5; // true
10.5 >= 5; // true
10.5 >= 5; // true
</syntaxhighlight>
</enforce>


==== &lt;{{=}} ====
==== &lt;{{=}} ====
Line 155: Line 155:
10 <= 5.5; // false
10 <= 5.5; // false
10.5 <= 5; // false
10.5 <= 5; // false
</syntaxhighlight>
</enforce>


==== {{=}}{{=}} ====
==== {{=}}{{=}} ====
Line 187: Line 187:
MyClass object2 = new MyClass();
MyClass object2 = new MyClass();
object1 == object2; // false - same type, different instance
object1 == object2; // false - same type, different instance
</syntaxhighlight>
</enforce>


==== !{{=}} ====
==== !{{=}} ====
Line 216: Line 216:
MyClass object2 = new MyClass();
MyClass object2 = new MyClass();
object1 != object2; // true - different instance
object1 != object2; // true - different instance
</syntaxhighlight>
</enforce>


=== Logical ===
=== Logical ===
Line 234: Line 234:
boolean b = true;
boolean b = true;
boolean result = a && b; // result is true
boolean result = a && b; // result is true
</syntaxhighlight>
</enforce>


==== {{!}}{{!}} ====
==== {{!}}{{!}} ====
Line 250: Line 250:
boolean b = true;
boolean b = true;
boolean result = a || b; // result is true
boolean result = a || b; // result is true
</syntaxhighlight>
</enforce>


==== ! ====
==== ! ====
Line 297: Line 297:
Print("instance is NOT null!");
Print("instance is NOT null!");
}
}
</syntaxhighlight>
</enforce>


=== Bitwise ===
=== Bitwise ===
Line 308: Line 308:
int result = a & b; // 00001001 in binary
int result = a & b; // 00001001 in binary
// result is 9
// result is 9
</syntaxhighlight>
</enforce>


==== {{!}} ====
==== {{!}} ====
Line 317: Line 317:
int result = a | b; // 00001111 in binary
int result = a | b; // 00001111 in binary
// result is 15
// result is 15
</syntaxhighlight>
</enforce>


==== ~ ====
==== ~ ====
Line 325: Line 325:
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
</syntaxhighlight>
</enforce>


==== &lt;&lt; ====
==== &lt;&lt; ====
Line 336: Line 336:
// result is 00000100
// result is 00000100
// result is 4
// result is 4
</syntaxhighlight>
</enforce>


==== &gt;&gt; ====
==== &gt;&gt; ====
Line 347: Line 347:
// result is 00000001
// result is 00000001
// result is 1
// result is 1
</syntaxhighlight>
</enforce>


=== String ===
=== String ===
Line 374: Line 374:
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
</syntaxhighlight>
</enforce>


=== Indexing ===
=== Indexing ===
Line 390: Line 390:
string text = "ABCDE";
string text = "ABCDE";
string thirdLetter = text[2]; // result is "C"
string thirdLetter = text[2]; // result is "C"
</syntaxhighlight>
</enforce>





Revision as of 19:18, 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.

int five = 5;

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.

int result = 5; result++; // result is 6

--

The decrement operator removes 1 to the provided numerical variable. It only applies to int and float values.

int result = 5; result--; // result is 4

+=

The "add to" operator adds the righthand value to the lefthand variable.

int result = 5; result += 3; // result is 8

-=

The "subtract from" operator subtract the righthand value to the lefthand variable.

int result = 5; result -= 3; // result is 2

*=

The "multiply with" operator multiplies the lefthand variable with the righthand value.

int result = 5; result *= 3; // result is 15

/=

The "divide by" operator multiplies the lefthand variable with the righthand value.

int result = 5; result /= 3; // result is 1 (floored int value)

Arithmetic

+

he "adds" operator does an addition between two numeric values, float and/or integer.

5 + 3; // result is 8 (int) 5 + 2.5; // result is 7.5 (float) 3.25 + 1.75; // result is 5.0 (float)

-

The "subtract from" operator does a subtraction between two numerical values, float and/or integer.

5 - 3; // result is 2 (int) 5 - 2.5; // result is 2.5 (float) 3.25 - 1.25; // result is 2.0 (float)

*

The multiplication sign does a multiplication between two numerical values, float and/or integer.

5 * 3; // result is 15 5 * 2.5; // result is 12.5 (float) 1.5 * 3.25; // result is 4.875 (float)

/

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.

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)

%

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.}}

5 % 3; // result is 2: 5 is one time 3, remaining 2 10 % 2; // result is 0: 10 is five times 2, remaining 0


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.

10 > 11; // false 10 > 10; // false 10 > 9; // true 10 > 5; // true 10 > 5.5; // true 10.5 > 5; // true

<

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.

10 < 11; // true 10 < 10; // false 10 < 9; // false 10 < 5; // false 10 < 5.5; // false 10.5 < 5; // false

>=

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.

10 >= 11; // false 10 >= 10; // true 10 >= 9; // true 10 >= 5; // true 10 >= 5.5; // true 10.5 >= 5; // true

<=

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.

10 <= 11; // true 10 <= 10; // true 10 <= 9; // false 10 <= 5; // false 10 <= 5.5; // false 10.5 <= 5; // false

==

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)

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

!=

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)

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

Logical

&&

The "and" operator returns true if both left and right conditions are true.

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

||

The "or" logical operator returns true if left or right condition is true (or both).

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

!

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)

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!"); }

Bitwise

&

The "bitwise AND" operator applies a AND operation between values bits - both bits must be 1 to return 1, otherwise 0 is returned.

int a = 13; // 00001101 in binary int b = 11; // 00001011 in binary int result = a & b; // 00001001 in binary // result is 9

|

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.

int a = 13; // 00001101 in binary int b = 11; // 00001011 in binary int result = a | b; // 00001111 in binary // result is 15

~

The "bitwise NOT" operator reverts all bits of the provided value: 1 becomes 0 and vice-versa.

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

<<

The left shift operator moves all bits of the provided value to the left.

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

>>

The right shift operator moves bits of the provided value to the right.

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

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.

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

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.

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"


Precedence

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