Operators

From TrainzOnline
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
(Miscellaneous adjustments.)
Line 1: Line 1:
 
The operator sections set out below follow the language's preference rules. The earlier the operator appears in the list, the higher the precedence and the earlier the listed operators will be evaluated.__NOEDITSECTION__
 
The operator sections set out below follow the language's preference rules. The earlier the operator appears in the list, the higher the precedence and the earlier the listed operators will be evaluated.__NOEDITSECTION__
 
  
  
Line 24: Line 23:
 
*Array and String range index.  
 
*Array and String range index.  
 
*The expression ''string[0,string.size()]'' will return the entire string.   
 
*The expression ''string[0,string.size()]'' will return the entire string.   
*The integers i and j are best thought of as 'cursor' positions where 0 is before the first element and size() is after the last element.  
+
*The integers i and j are best thought of as 'cursor' positions where i is the first element and j is one beyond the last element.  
*If i is omitted the range will start at the beginning of the array,
+
*If i is omitted the range will start at the beginning of the array (index 0).
*If j is omitted the range will end after the end of the array.  
+
*If j is omitted the range will end after the end of the array (.size()).  
*To return a single element both i and j are required.  
+
*To return a single element other than the first or last, both i and j are required.  
 
*Given the declaration ''string s = "abcde"''
 
*Given the declaration ''string s = "abcde"''
 
  s[,3]  = "abc"
 
  s[,3]  = "abc"
Line 38: Line 37:
 
{{MethodHeader|[ i , ]}}
 
{{MethodHeader|[ i , ]}}
 
*Indexes from i to the end of the string or array inclusively.
 
*Indexes from i to the end of the string or array inclusively.
 +
s[2,]  = "cde"
  
  
 
{{MethodHeader|[ , j ]}}
 
{{MethodHeader|[ , j ]}}
*Indexes from the start of the string or array to element b inclusively.
+
*Indexes from the start of the string or array to element j-1 inclusively.
 +
s[,3]  = "abc"
  
  
 
{{MethodHeader|.}}
 
{{MethodHeader|.}}
*Dereference operator as in ''GetAsset().GetConfigSoup();''
+
*Dereference operator
 +
GetAsset().GetConfigSoup();
 +
*The ''GetConfigSoup()'' method to be called is the method defined for the class ''GetAsset()''.
  
  
Line 52: Line 55:
 
  !true = false
 
  !true = false
 
  !vehicle = null
 
  !vehicle = null
 +
  
 
==Operators (2), Incremental and Unary Operators==
 
==Operators (2), Incremental and Unary Operators==
Line 90: Line 94:
 
  (bool) 0.0 = false
 
  (bool) 0.0 = false
 
  (bool) n = true    //where n is greater than zero
 
  (bool) n = true    //where n is greater than zero
 +
  
 
{{MethodHeader|<h5>(int)</h5>}}
 
{{MethodHeader|<h5>(int)</h5>}}
Line 95: Line 100:
 
  (int) 5.0 = 5
 
  (int) 5.0 = 5
 
  (int) true = 1
 
  (int) true = 1
 +
  
 
{{MethodHeader|<h5>(float)</h5>}}
 
{{MethodHeader|<h5>(float)</h5>}}
Line 100: Line 106:
 
  (float) 100 = 100.0
 
  (float) 100 = 100.0
 
  (float) false = 0.0
 
  (float) false = 0.0
 +
  
 
{{MethodHeader|<h5>(string)</h5>}}
 
{{MethodHeader|<h5>(string)</h5>}}
Line 106: Line 113:
 
  (string) 0.99 = "0.99"
 
  (string) 0.99 = "0.99"
 
  (string) false = "false";
 
  (string) false = "false";
 +
  
 
{{MethodHeader|(''reference'')}}
 
{{MethodHeader|(''reference'')}}
 
*Class type cast.
 
*Class type cast.
 
  <font color=red>How is this used and how is it different from cast<Class>?</font>
 
  <font color=red>How is this used and how is it different from cast<Class>?</font>
 +
  
 
{{MethodHeader|<h5>cast</h5>}}
 
{{MethodHeader|<h5>cast</h5>}}
Line 119: Line 128:
 
  MeshObject meshObject;
 
  MeshObject meshObject;
 
  GameObject gameObject = cast<GameObject>meshObject;  // returns meshObject if the object exists, null otherwise
 
  GameObject gameObject = cast<GameObject>meshObject;  // returns meshObject if the object exists, null otherwise
 +
  
 
{{MethodHeader|<h5>size()</h5>}}
 
{{MethodHeader|<h5>size()</h5>}}
Line 128: Line 138:
 
  int[] numbers = new int[5];      // creates an empty array of 5 integers
 
  int[] numbers = new int[5];      // creates an empty array of 5 integers
 
  numbers.size() = 5;
 
  numbers.size() = 5;
 +
  
 
{{MethodHeader|<h5>copy()</h5>}}
 
{{MethodHeader|<h5>copy()</h5>}}
Line 137: Line 148:
 
   
 
   
 
  moreNumbers.copy(numbers);  // duplicates numbers  
 
  moreNumbers.copy(numbers);  // duplicates numbers  
 +
  
 
{{MethodHeader|<h5>isclass()</h5>}}
 
{{MethodHeader|<h5>isclass()</h5>}}
Line 148: Line 160:
 
   
 
   
 
  info.isclass(TrackLoadInfo);              // returns true
 
  info.isclass(TrackLoadInfo);              // returns true
 +
  
 
{{MethodHeader|<h5>new</h5>}}
 
{{MethodHeader|<h5>new</h5>}}
Line 155: Line 168:
 
*new does not initialise the members.
 
*new does not initialise the members.
 
*GameObject classes cannot be created using new.
 
*GameObject classes cannot be created using new.
 +
  
 
==Operators (4), Multiplication & Division==
 
==Operators (4), Multiplication & Division==
Line 163: Line 177:
 
  2 * 5 = 10;
 
  2 * 5 = 10;
 
  2 * 5.0 = 10.0;
 
  2 * 5.0 = 10.0;
 +
  
 
{{MethodHeader|/}}
 
{{MethodHeader|/}}
Line 171: Line 186:
 
  10 / 5.0 = 2.0;
 
  10 / 5.0 = 2.0;
 
  10 / 6 = 1;
 
  10 / 6 = 1;
 +
  
 
{{MethodHeader|%}}
 
{{MethodHeader|%}}
Line 178: Line 194:
 
  105 % 50 = 5;
 
  105 % 50 = 5;
 
  105 % 100 = 5;
 
  105 % 100 = 5;
 +
  
 
==Operators (5), Addition & Subtraction==
 
==Operators (5), Addition & Subtraction==
Line 186: Line 203:
 
  1 + 1 = 2;
 
  1 + 1 = 2;
 
  1 + 1.0 = 2.0;
 
  1 + 1.0 = 2.0;
 +
  
 
{{MethodHeader|-}}
 
{{MethodHeader|-}}
Line 193: Line 211:
 
  2 - 1 = 1;
 
  2 - 1 = 1;
 
  2 - 1.0 = 1.0;
 
  2 - 1.0 = 1.0;
 +
  
 
==Operators (6), Shifts==
 
==Operators (6), Shifts==
Line 202: Line 221:
 
  8 << 1 = 16
 
  8 << 1 = 16
 
  8 << 2 = 32
 
  8 << 2 = 32
 +
  
 
{{MethodHeader|>>}}
 
{{MethodHeader|>>}}
Line 210: Line 230:
 
  8 >> 1 = 4
 
  8 >> 1 = 4
 
  9 >> 2 = 2
 
  9 >> 2 = 2
 +
  
 
==Operators (7), Comparisons==
 
==Operators (7), Comparisons==
 
{{MethodHeader|<}}
 
{{MethodHeader|<}}
 +
* Is less than.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Returns true if the first term is less than the second.
 
*Returns true if the first term is less than the second.
Line 218: Line 240:
 
  false < false = false;
 
  false < false = false;
 
  "abc" < "def" = true;
 
  "abc" < "def" = true;
 +
  
 
{{MethodHeader|<&#061;}}
 
{{MethodHeader|<&#061;}}
 +
*Is less than or equal.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Returns true if the first term is less than or equal to the second.
 
*Returns true if the first term is less than or equal to the second.
Line 225: Line 249:
 
  false <= false = true;
 
  false <= false = true;
 
  "abc" <= "def" = true;
 
  "abc" <= "def" = true;
 +
  
 
{{MethodHeader|>}}
 
{{MethodHeader|>}}
 +
*Is greater than.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Returns true if the first term is greater than the second.
 
*Returns true if the first term is greater than the second.
Line 232: Line 258:
 
  true > false = true;
 
  true > false = true;
 
  "abc" > "def" = false;
 
  "abc" > "def" = false;
 +
  
 
{{MethodHeader|>&#061;}}
 
{{MethodHeader|>&#061;}}
 +
*Is greater than or equal.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Comparison operator which can be used on string, float, integer and bool values.
 
*Returns true if the first term is greater than or equal to the second.
 
*Returns true if the first term is greater than or equal to the second.
Line 239: Line 267:
 
  true >= true = true;
 
  true >= true = true;
 
  "abc" >= "def" = false;
 
  "abc" >= "def" = false;
 +
  
 
==Operators (8), Equality==
 
==Operators (8), Equality==
 
{{MethodHeader|&#061;&#061;}}
 
{{MethodHeader|&#061;&#061;}}
 +
*Is equal.
 
*Comparison operator which can be used on string, float, integer and bool values and on object references.
 
*Comparison operator which can be used on string, float, integer and bool values and on object references.
 
*Returns true if the first and second terms are equal or if they evaluate to the same object.
 
*Returns true if the first and second terms are equal or if they evaluate to the same object.
Line 248: Line 278:
 
  a == b;  // returns true
 
  a == b;  // returns true
 
*Note that ''a = b'' is an assignment and will always return true. Mistyping '''''=''''' for '''''==''''' is one of the most common semantic errors in Trainzscript.
 
*Note that ''a = b'' is an assignment and will always return true. Mistyping '''''=''''' for '''''==''''' is one of the most common semantic errors in Trainzscript.
 +
  
 
{{MethodHeader|!&#061;}}
 
{{MethodHeader|!&#061;}}
 +
*Is not equal.
 
*Comparison operator which can be used on string, float, integer and bool values and on object references.
 
*Comparison operator which can be used on string, float, integer and bool values and on object references.
 
*Returns true if the first and second terms are not equal and do not evaluate to the same object.
 
*Returns true if the first and second terms are not equal and do not evaluate to the same object.
Line 255: Line 287:
 
  int b = 0;
 
  int b = 0;
 
  a != b;  // returns false
 
  a != b;  // returns false
 +
  
 
==Operators (9), Bitwise Operators==
 
==Operators (9), Bitwise Operators==
Line 266: Line 299:
  
 
  15 & 23 = 7
 
  15 & 23 = 7
 +
  
 
{{MethodHeader|^}}
 
{{MethodHeader|^}}
Line 276: Line 310:
  
 
  15 ^ 23 = 24
 
  15 ^ 23 = 24
 +
  
 
{{MethodHeader|&#166;}}
 
{{MethodHeader|&#166;}}
Line 286: Line 321:
  
 
  15 | 23 = 31
 
  15 | 23 = 31
 +
  
 
{{MethodHeader|~}}
 
{{MethodHeader|~}}
*Bitwise Inverse Operator for integer values
+
*Bitwise Inverse
 
*Takes the binary representation of the integer and inverts each bit.
 
*Takes the binary representation of the integer and inverts each bit.
 
*Useful in masking out values from bitfields (eg.  flags &= ~SOME_FLAG;  switches off the SOME_FLAG bit from 'flags')
 
*Useful in masking out values from bitfields (eg.  flags &= ~SOME_FLAG;  switches off the SOME_FLAG bit from 'flags')
Line 297: Line 333:
 
  ~255 = -256
 
  ~255 = -256
 
  ~5  = -6
 
  ~5  = -6
 +
  
 
==Operators (10), Logical Operators==
 
==Operators (10), Logical Operators==
Line 303: Line 340:
 
*Enables multiple boolean expressions to be combined.
 
*Enables multiple boolean expressions to be combined.
 
  if (msg.minor == "Enter" and doorsOpen)
 
  if (msg.minor == "Enter" and doorsOpen)
 +
  
 
{{MethodHeader|<h5>or</h5>}}
 
{{MethodHeader|<h5>or</h5>}}
Line 309: Line 347:
 
  if (msg.minor == "Enter" or doorsOpen)
 
  if (msg.minor == "Enter" or doorsOpen)
 
  if ((msg.minor == "Enter" and doorsOpen) or (msg.minor == "Leave" and !doorsOpen))
 
  if ((msg.minor == "Enter" and doorsOpen) or (msg.minor == "Leave" and !doorsOpen))
 +
  
 
==Operators (11), Assignment==
 
==Operators (11), Assignment==
Line 316: Line 355:
 
  Train train = World.GetTrainList()[0];
 
  Train train = World.GetTrainList()[0];
  
Apparently there are 2 types in Trainz gamescript that don't support direct assignment of themselves: arrays and strings. When arrays and strings appear singlely on the right hand side in assignment statements only a reference to the original is created. So in effect only the original copy exists. The original name and the new name both reference the same object. To make an assignment to a new object that is a copy of the original you can use the copy() operator.
+
The assignment operator sets the variable on the LHS of the expression to the same value as the variable on the RHS.
  
 +
Note that string and array are object types, not primitive types.  Therefore, like all objects, assignment of a string or array object means that the variable on the LHS of the assignment expression refers to the same object (that is, the same string or the same array) as the variable on the RHS. A new copy of the object is not created.  If the intention is to make an assignment to a new object that is a copy of the original, you should use the .copy() method of the string or array objects, or the .clone() method (if available) of other objects.
  
  
 
{{MethodHeader|,}}
 
{{MethodHeader|,}}
 
*Expression or parameter separator.
 
*Expression or parameter separator.

Revision as of 10:45, 24 April 2019

The operator sections set out below follow the language's preference rules. The earlier the operator appears in the list, the higher the precedence and the earlier the listed operators will be evaluated.


Contents

Operators (1), Grouping & Indexing

( )
  • Expression grouping.
  • In common with most programming languages Trainzscript carries out arithmetical operations in the order defined by operator precedence rules.
  • 3 + 2 * 12 - 7 would return 20, because multiplication is of higher precedence than addition and subtraction.
  • You can force the compiler to change this sequence and make your intentions crystal clear by declaring this as ((3 + 2) * 12) - 7.


[ i ]
  • Array and string indexing.
  • Arrays are zero based and arrayName[0] will represent the first element.
  • Similarly arrayName[arrayName.size() - 1] will represent the last element.
  • A call to a value outside the range [0 … size()-1] will generate an exception.
  • This notation can also be used to return a single character from a string.
intArray[intArray.size()] = 5    // will add the new value of 5 to the end of the array.


[ i , j ]
  • Array and String range index.
  • The expression string[0,string.size()] will return the entire string.
  • The integers i and j are best thought of as 'cursor' positions where i is the first element and j is one beyond the last element.
  • If i is omitted the range will start at the beginning of the array (index 0).
  • If j is omitted the range will end after the end of the array (.size()).
  • To return a single element other than the first or last, both i and j are required.
  • Given the declaration string s = "abcde"
s[,3]  = "abc"
s[2,3] = "c"
s[2,]  = "cde"
intArray[2,3] = null             // will delete the third element of the array.


[ i , ]
  • Indexes from i to the end of the string or array inclusively.
s[2,]  = "cde"


[ , j ]
  • Indexes from the start of the string or array to element j-1 inclusively.
s[,3]  = "abc"


.
  • Dereference operator
GetAsset().GetConfigSoup();
  • The GetConfigSoup() method to be called is the method defined for the class GetAsset().


!
  • Not Operator
!true = false
!vehicle = null


Operators (2), Incremental and Unary Operators

++
  • Increments a numeric value by 1.
  • Often used for control variables in loops:
for(n = 0; n < list.size(); n++) {
   DoStuffWith(n);
}
  • Increments n by 1 on each pass through the loop.


--
  • Decrements a numeric value by 1.
  • Often used for control variables in loops:
n = list.size() - 1;
while(n >= 0) {
   DoStuffWith(n);
   n--;
}
  • Decrements n by 1 on each pass through the loop.


+
  • Unary plus.
  • Used to denote that a value is positive.


-
  • Unary minus.
  • Used to denote that a value is negative.


Operators (3), Casts

(bool)
  • Boolean type cast.
(bool) 0 = false
(bool) 0.0 = false
(bool) n = true     //where n is greater than zero


(int)
  • Integer type cast.
(int) 5.0 = 5
(int) true = 1


(float)
  • Float type cast.
(float) 100 = 100.0
(float) false = 0.0


(string)
  • String type cast.
(string) 1 = "1"
(string) 0.99 = "0.99"
(string) false = "false";


(reference)
  • Class type cast.
How is this used and how is it different from cast<Class>?


cast
  • Dynamic cast.
  • Returns null if the reference cannot be converted to the class specified by className.
  • Note that ancestors of the specified class will be returned.
Vehicle vehicle = cast<Vehicle>msg.src;

MeshObject meshObject;
GameObject gameObject = cast<GameObject>meshObject;  // returns meshObject if the object exists, null otherwise


size()
  • Returns the number of members of an array or the length of a string.
  • In the case of an array this does not necessarily mean that all (or any) of the members actually contain values.
given string s = "abcde";
s.size() = 5;

int[] numbers = new int[5];       // creates an empty array of 5 integers
numbers.size() = 5;


copy()
  • Creates a copy of an array or string.
  • This is different from a simple assignment which points a variable to the same memory address as an existing variable.
  • Array.copy() or string.copy() creates an additional copy of the data.
int[] numbers = new int[5];
int[] moreNumbers;

moreNumbers.copy(numbers);  // duplicates numbers 


isclass()
  • Used in the declaration of a new class identifier
class MyScript isclass Vehicle {
  classMethods();
};
  • Used to test whether a given reference is to a specific class.
given the declaration: 
TrackLoadInfo info = new TrackLoadInfo();

info.isclass(TrackLoadInfo);               // returns true


new
  • Creates a new instance of the specified type.
int[] numbers = new int[4];                // creates an integer array containing 4 members. 
TrackLoadInfo info = new TrackLoadInfo();  // creates a new instance of the TrackLoadInfo class.
  • new does not initialise the members.
  • GameObject classes cannot be created using new.


Operators (4), Multiplication & Division

*
  • Multiplication.
  • If both terms are integers the result will be an integer.
  • If either term is a float the result will be a float.
2 * 5 = 10;
2 * 5.0 = 10.0;


/
  • Division.
  • If both terms are integers the result will be an integer, possibly with a remainder.
  • If either term is a float the result will be a float.
10 / 5 = 2;
10 / 5.0 = 2.0;
10 / 6 = 1;


%
  • Remainder or modulus operator.
  • Returns the remainder from an integer division.
5 % 2 = 1;
105 % 50 = 5;
105 % 100 = 5;


Operators (5), Addition & Subtraction

+
  • Addition
  • If both terms are integers the result will be an integer.
  • If either term is a float the result will be a float.
1 + 1 = 2;
1 + 1.0 = 2.0;


-
  • Subtraction
  • If both terms are integers the result will be an integer.
  • If either term is a float the result will be a float.
2 - 1 = 1;
2 - 1.0 = 1.0;


Operators (6), Shifts

<<
  • Shift Left
  • Bitwise operator moves all of the binary values to the left by the number of places specified in the second term.
  • Binary zero is appended.
  • Provided there is no overflow the result is a doubling of the value for each shift.
8 << 1 = 16
8 << 2 = 32


>>
  • Shift Right
  • Bitwise operator moves all of the binary values one place to the right by the number of places specified in the second term.
  • Binary zero is prepended and any displaced bits are discarded.
  • The result is an integral halving of the value for each shift.
8 >> 1 = 4
9 >> 2 = 2


Operators (7), Comparisons

<
  • Is less than.
  • Comparison operator which can be used on string, float, integer and bool values.
  • Returns true if the first term is less than the second.
1 < 2 = true;
false < false = false;
"abc" < "def" = true;


<=
  • Is less than or equal.
  • Comparison operator which can be used on string, float, integer and bool values.
  • Returns true if the first term is less than or equal to the second.
1 <= 2 = true;
false <= false = true;
"abc" <= "def" = true;


>
  • Is greater than.
  • Comparison operator which can be used on string, float, integer and bool values.
  • Returns true if the first term is greater than the second.
1 > 2 = false;
true > false = true;
"abc" > "def" = false;


>=
  • Is greater than or equal.
  • Comparison operator which can be used on string, float, integer and bool values.
  • Returns true if the first term is greater than or equal to the second.
1 >= 2 = false;
true >= true = true;
"abc" >= "def" = false;


Operators (8), Equality

==
  • Is equal.
  • Comparison operator which can be used on string, float, integer and bool values and on object references.
  • Returns true if the first and second terms are equal or if they evaluate to the same object.
int a = 0;
int b = 0;
a == b;  // returns true
  • Note that a = b is an assignment and will always return true. Mistyping = for == is one of the most common semantic errors in Trainzscript.


!=
  • Is not equal.
  • Comparison operator which can be used on string, float, integer and bool values and on object references.
  • Returns true if the first and second terms are not equal and do not evaluate to the same object.
int a = 0;
int b = 0;
a != b;  // returns false


Operators (9), Bitwise Operators

&
  • Bitwise AND
  • Performs a bit by bit comparison of two integers and returns a new value.
  • Each bit in the result is set if BOTH of the corresponding bits in the arguments are set
  0000 1111
& 0001 0111
= 0000 0111
15 & 23 = 7


^
  • Bitwise XOR
  • Performs a bit by bit comparison of two integers and returns a new value.
  • Each bit in the result is set if EITHER BUT NOT BOTH of the corresponding bits in the arguments are set
  0000 1111
^ 0001 0111
= 0001 1000
15 ^ 23 = 24


¦
  • Bitwise OR
  • Performs a bit by bit comparison of two integers and returns a new value.
  • Each bit in the result is set if EITHER of the corresponding bits in the arguments are set
  0000 1111
| 0001 0111
= 0001 1111
15 | 23 = 31


~
  • Bitwise Inverse
  • Takes the binary representation of the integer and inverts each bit.
  • Useful in masking out values from bitfields (eg. flags &= ~SOME_FLAG; switches off the SOME_FLAG bit from 'flags')
~0011 1101 = 1100 0010
~1000 0100 = 0111 1011
~0   = -1
~255 = -256
~5   = -6


Operators (10), Logical Operators

and
  • Logical AND
  • Enables multiple boolean expressions to be combined.
if (msg.minor == "Enter" and doorsOpen)


or
  • Logical OR
  • Enables multiple boolean expressions to be combined.
if (msg.minor == "Enter" or doorsOpen)
if ((msg.minor == "Enter" and doorsOpen) or (msg.minor == "Leave" and !doorsOpen))


Operators (11), Assignment

=
  • Assignment Operator
a = 1;
Train train = World.GetTrainList()[0];

The assignment operator sets the variable on the LHS of the expression to the same value as the variable on the RHS.

Note that string and array are object types, not primitive types. Therefore, like all objects, assignment of a string or array object means that the variable on the LHS of the assignment expression refers to the same object (that is, the same string or the same array) as the variable on the RHS. A new copy of the object is not created. If the intention is to make an assignment to a new object that is a copy of the original, you should use the .copy() method of the string or array objects, or the .clone() method (if available) of other objects.


,
  • Expression or parameter separator.
Personal tools