gcs-lang.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <P><PRE>
  2. **
  3. ** GCSx language reference
  4. ** Original (incomplete) revision: August 22, 2006 Alexis Janson
  5. **
  6. </PRE></P>
  7. <P>
  8. TODO: constants, states, libraries w/::, (true/false/)query/nothing, comments, #names, with (and this), source, void, string subscripting<BR>
  9. future: extend (inheritance), import, public vs private including default<BR>
  10. built-in methods, properties, functions
  11. how to enable/disable features like automatic-stringification-of-entity-names
  12. array<->hash conversion?
  13. debug cmds (debug, trace, breakpoint, watch)
  14. </P>
  15. <H1>Introduction</H1>
  16. <P>
  17. The goal of this document is to describe all functionality of the GCSx scripting language in plain english. This document will not define language structure in a technically exact manner, but should suffice to explain it in a complete and unambiguous capacity. It is not intended to be a tutorial and assumes the reader is familiar with basic programming concepts.
  18. </P>
  19. <H1>Terminology</H1>
  20. <P><UL>
  21. <LI>Script: A program, without any in-game state information attached.
  22. <LI>Entity: An instance of a script, with it's own state information such as a stack and variables. Multiple entities can exist running a single script.
  23. <LI>Sprite: An on-screen graphical representation, often associated one-to-one with an entity.
  24. <LI>Object: Any in-game resource that you can manipulate in a script, such as a scene, an entity, or a sprite.
  25. <LI>Object Type: A type of object, such as script, scene, entity, or entities running a specific script.
  26. <LI>Script Type: A specific subset of object types, a script type matches all entities running a specific script.
  27. </UL></P>
  28. <H1>Basic Code Elements</H1>
  29. <H2>Keywords</H2>
  30. <P>The following keywords are reserved for current, planned, or potential use:</P>
  31. <P><PRE>all
  32. all_other
  33. and
  34. array
  35. as
  36. break
  37. case (unused)
  38. const
  39. continue
  40. default
  41. do
  42. else
  43. end
  44. extend
  45. false
  46. float
  47. for
  48. foreach
  49. global
  50. hash
  51. if
  52. import
  53. in
  54. int
  55. is
  56. local
  57. query
  58. not
  59. nothing
  60. obj_* (any word beginning with 'obj_')
  61. or
  62. private
  63. public
  64. repeat
  65. reply
  66. restart
  67. return
  68. source
  69. state
  70. static (unused)
  71. str
  72. switch (unused)
  73. this
  74. to
  75. true
  76. until
  77. var
  78. void
  79. while
  80. with
  81. xor</PRE></P>
  82. <P>All other words can be used as identifiers. Some words may be removed off this list once the language stabilizes. Keywords are not case-sensitive.</P>
  83. <H2>Identifiers</H2>
  84. <P>An identifier is a sequence of letters, numbers, and underscores, and cannot begin with a number. An identifier cannot match a built-in keyword. Identifiers are not case-sensitive. Identifiers are used to represent all user-defined items such as variables, functions, etc. as well as built-in variables, functions, etc.</P>
  85. <H1>Data Types</H1>
  86. <P>Each data type has an associated empty value, which is used whenever a variable of that type is uninitialized, or a result of that type is needed, but could not be generated. Occasionally the concept of an "undefined" value will be used- this simply means a value of the appropriate type but unknown content. For example, an undefined integer would still be an integer, but it could be any valid integer within the entire range of all possible integers.</P>
  87. <H2>Integers</H2>
  88. <P>Integers are denoted with the keyword 'int'. Integers can be written in hexadecimal as well, with the 0x prefix- 0xABCD. Integers are stored using exactly 32 bits. The empty integer value is 0.</P>
  89. <H2>Floating-point</H2>
  90. <P>Floating-point numbers are denoted with the keyword 'float'. The empty floating-point value is 0.0. Scientific notation is not currently supported. Floating-point numbers currently have a non-defined level of precision.</P>
  91. <H2>Strings</H2>
  92. <P>Strings are a primitive data type, denoted with the keyword 'str'. A string can be any length and contain any non-NUL characters. Strings do not currently support Unicode.</P>
  93. <P>Strings are written between "double-quotes". Use \t to represent a tab, \n for a newline, \\ for a backslash, and \" to enclose a double-quote. All other characters except NUL (ASCII 0) can be inserted directly.</P>
  94. <P>The empty string value is "".</P>
  95. <H2>Object Types</H2>
  96. <P>Each type of game resource that can be referenced from within a script is called an object. However, an object is not a data type- only a specific type of resource or object is a data type. In otherwords, a scene is a data type, but there is no generic catchall object data type. This is similar to how you cannot simply have a class variable in C++, but must specify which type of class it is.</P>
  97. <P>Object types that currently exist as data types:</P>
  98. <P><UL>
  99. <LI>obj_scene
  100. <LI>obj_sprite
  101. <LI>obj_entity
  102. </UL></P>
  103. <P>These data type keywords were chosen over shorter names (without 'obj_') to avoid conflicts with desirable variable names and because an average script will not need to refer to these data types explicitly on a regular basis.</P>
  104. <P>The empty object value is represented by the keyword 'nothing'. Using a 'nothing' object under most circumstances simply fails to do anything. Specific results are covered for each operator and function that works with objects.</P>
  105. <P>All script names can also be used as object types- these are basically subtypes of the broader obj_entity type and are referred to as "Script Types". When using a script type, it must be preceded by another data type keyword (such as 'obj_entity' or 'local') or the keyword 'as' or 'is' to clarify it is meant as an object type name and not a regular identifier. You can also enclose a script type in 'single quotes' to clarify it is a script type. This is the only way to refer to script names that contain spaces or do not match normal identifier rules. Also note that script type names are not case sensitive.</P>
  106. <H2>Arrays</H2>
  107. <P>An array must be of a specific data type previously mentioned, combined with the 'array' keyword. For example, 'int array' or 'array obj_scene'. An array contains zero or more of the specified data type, and dynamically grows as necessary.</P>
  108. <P>Attempting to read from a non-existant array location simply returns an empty value of the appropriate type. Attempting to write to a non-existant array location expands the array to the required size, filling any interim elements with empty values.</P>
  109. <P>Array literals are written as comma-separated lists between [ brackets ]. For example, [ 0, 1, 2 ]. Indexing into an array also uses brackets, containing a numeric index. For example, array_name[1]. Nested or multidimensional arrays are not natively supported.</P>
  110. <P>The empty array type is [ ], for any array type.</P>
  111. <H2>Hashes</H2>
  112. <P>A hash, or associative array, must be of a specific data type previously mentioned, combined with the 'hash' keyword. For example, 'str hash' or 'hash float'. A hash contains zero or more of the specified data type, referenced using arbitrary user-defined strings. Hash keys are case-sensitive.</P>
  113. <P>Attempting to read from a non-existant hash element simply returns an empty value of the appropriate type. Attempting to write to a non-existant hash element creates that element.</P>
  114. <P>Hashes cannot be written as literals. Accessing an individual hash element uses brackets, containing a string index. For example, hash_name["color"]. Nested or multidimensional hashes are not natively supported.</P>
  115. <P>The empty hash type is an empty hash with no elements.</P>
  116. <H2>Variant</H2>
  117. <P>Variants can contain any of the previously mentioned data types, denoted with the keyword 'var'. Variants can contain hashes or arrays, but hashes and arrays cannot contain variants. However, a variant can be restricted to containin hashes or arrays only by use of the type 'var array' or 'var hash'. This means that the variant can contain any specific type of array (or hash).</P>
  118. <P>In usage, a variant is somewhat restricted as you must declare it's type whenever it is used in an ambiguous context. For example, concatenating two variants will simply use the variants as strings, and assigning a variant to another variable will simply convert to that variable's type. However, adding two variants will require you to first declare one variant as floating point or BOTH variants as integers. (this is done using the 'as' keyword)</P>
  119. <P>Variants follow normal type conversion rules when possible; when not possible, empty values are used. An empty variant defaults to the integer 0, or if a hash or array variant, an empty integer hash or array.</P>
  120. <H2>Type conversion</H2>
  121. <P>Floating-point numbers automatically convert to integers and vice-versa as needed. (when an operation is performed between a floating-point and an integer, the result will be floating-point.)</P>
  122. <P>Both numeric types automatically convert to strings as needed. Strings manually convert to numbers using the 'as' operator, although a failed conversion will simply result in 0 or 0.0. When converting strings to numbers, whitespace is discarded off the front of the string, and any unrecognized characters are ignored on the end of the string. Exponents and non-base-10 numbers are not supported.</P>
  123. <P>An array or hash containing integers, floating-point numbers, or strings will automatically convert to the same structure containing a different type. The 'as' operator must be used if converting from strings to numbers.</P>
  124. <P>Different script object types can be freely and automatically converted to the broader obj_entity data type. obj_entity objects can be converted to more specific script object types by request, although if the object does not reference an object of the specified script, it will revert to a 'nothing' reference. These same rules apply to arrays or hashes containing obj_entity or script object types.</P>
  125. <P>No other type conversion between KNOWN types will be performed 'automatically'- if the compiler detects such an attempt, an error will result. For example, using a string where a number is expected without using 'as', or using a obj_scene where an obj_entity is required.</P>
  126. <P>Some situations involve UNKNOWN types, however- either due to use of variants, or unknown function parameters or return-values when communicating between entities. In these cases, conversion of strings to numbers is automatic at runtime, and if a standard conversion cannot be made, an empty value of the proper type is created instead, at runtime.</P>
  127. <H2>Booleans</H2>
  128. <P>Any value can be treated as a boolean value. Numeric 0, 0.0, "", an empty/nothing object, an empty hash, or an empty array are treated as false. All other values are treated as true. Specifically, "0" (a string) or [ 0 ] (an array containing zero) are true values.</P>
  129. <P>Built-in keywords 'true' and 'false' are equal to 1 and 0. All boolean operations, even if derived from other types or values, will result in an integer 1 or 0.</P>
  130. Code Structure
  131. With a few technical exceptions, scripts are a series of commands and blocks. A command is a single 'statement' of some sort, and can be terminated using a semicolon ; if desired. However, all commands are also terminated with end-of-line.
  132. Anywhere a single command can be used, a block of code may also be used. A code block is zero or more commands contained within { braces }. Braces automatically count as command terminators as well, if necessary.
  133. Functions
  134. Functions are defined by a data type, an identifier, an optional list of parameters, and a block of code-
  135. <PRE>int function(str a, str b) {
  136. // code goes here
  137. }</PRE>
  138. The parenthesis surrounding the parameter list are optional. A function with no parameters can be declared using () for a blank parameter list, or by simply not including a parameter list.
  139. Functions are called, in their simplest form, by using the identifier that represents the function, optionally followed by any parameters, optionally enclosed in parenthesis. The following are all legal function calls-
  140. <PRE>functionA
  141. functionB 1
  142. functionC 1, 2
  143. functionA()
  144. functionB(1)
  145. functionC(1, 2)</PRE>
  146. The compiler will generate an error if you do not use the proper number and type of parameters for a function. Built-in functions are called using the same syntax. Functions can also informally be referred to as members.
  147. The data type before a function name is called it's return type, and is the type of value that results when the function is called from within an expression. For example, a function that returns int can be used in an expression anywhere an integer value is allowed, and will be called at that time- the value it produces will then be used in the expression.
  148. A function returns a value by using the 'return' keyword, followed by the value to return. This immediately exits the function, returning the given value. (code after 'return' will never run- see the 'reply' keyword for an alternate that works in some situations.) If the end of the function is reached without a 'return' command, an empty value is returned. This can also be done if 'return' is used by itself, without a value.
  149. Variables
  150. Variables are defined by a data type with an optional scope modifier, an identifier, and an optional assignment-
  151. <PRE>local int variable = 10</PRE>
  152. Variables declared as 'local' can only be accessed from within the current block of code and any sub-blocks within it. This does not apply to functions called from within a block- only sub-blocks actually contained within a block can access that block's local variables.
  153. Local variables declared outside of any block are local to the entire script- any block of code, including functions, can access them. These variables are also the only variables that can be accessed by outside entities.
  154. Variables can also be declared as 'global'- these variables can be accessed by any script, and maintain only a single value shared by all scripts that access that variable. You must declare global variables in each script that wishes to access them, and you must always declare them with the same data type.
  155. Variables default to 'local' if not specified.
  156. Local variables outside of all blocks can be accessed by other entities, and are informally referred to as properties. You may optionally declare a local variable as 'private' to prevent it from being accessed by outside entities. This keyword has no effect on variables within blocks and may not be used with global variables.
  157. Note that variables and functions within the same script cannot share names.
  158. Expressions
  159. Expressions are anything that results in a value. Expressions use operators to combine literals, variables, and function calls into a single final result. Expressions can be as simple as '1' or more complex operations such as 'a + (b * 2 - sqrt(c))'. As shown, an expression is often made up of several smaller sub-expressions.
  160. Most operators take two expressions and return a single result; a few operators, known as unary operators, take a single expression and return a single result.
  161. Expressions are evaluated left-to-right, respecting operator precedence-
  162. Precedence 1: Parenthesis<BR>
  163. Precedence 2: . as<BR>
  164. Precedence 3: + - ~ not (all unary)<BR>
  165. Precedence 4: * / %<BR>
  166. Precedence 5: + -<BR>
  167. Precedence 6: &lt;&lt; &gt;&gt;<BR>
  168. Precedence 7: &amp; | ^<BR>
  169. Precedence 8: #<BR>
  170. Precedence 9: &lt; &gt; &lt;= &gt;=<BR>
  171. Precedence 10: == &lt;&gt; != is<BR>
  172. Precedence 11: and or xor<BR>
  173. Precedence 12: =
  174. Parenthesis: These work as expected, overriding all other operator precedence.
  175. '.': The dot . operator accesses a property or method of an object. More details are in the section covering objects.
  176. 'as': 'as' converts between data types. The first operand is temporarily converted to the second type, which must be a data type such as 'str' or 'obj_sprite array', or even a script type. When using 'as' on known types, only valid conversions are permitted. When using 'as' on unknown types such as variants, an unsupported conversion will result in an empty value of the requested type.
  177. '+', '-' unary: Unary + and - simply represent the positive and negative of a numeric value. + is essentially a no-op, whereas - returns the negation of a value.
  178. '~' unary: Tilde ~ returns the binary negation of an integer value, flipping all bits.
  179. 'not' unary: 'not' returns the logical (boolean) negation of a value, returning true if false, or false if true.
  180. '*', '/', '+', '-': These basic mathematical operations work as expected, operating on two numeric values. '/' will produce an integer result unless at least one value involved is floating-point- if you divide two integers, the answer will be rounded down. Dividing by zero results in an undefined value.
  181. '%': Modulo % returns the remainder after an integer division. Floating-point values are converted to integers before the operation. Modulo by zero results in an undefined value.
  182. '&lt;&lt;', '&gt;&gt;': Bit-shift operators take an integer and shift it left or right a given number of bits. Floating-point values are converted to integers first. Shifting right does not sign-extend.
  183. '&amp;', '|', '^': Bit operators return the binary AND &amp;, OR |, or XOR ^ of two integer values.
  184. '#': Concatenation # takes two strings and returns a single string containing the two strings joined.
  185. '&lt;', '&gt;', '&lt;=', '&gt;=', '==', '!=', '&lt;&gt;': Relational operators compare two values and return true (1) if they compare favorably or false (0) if they do not. In order, the operators are less-than, greater-than, less-than-or-equal, greater-than-or-equal, equal-to, and not-equal-to. Not-equal-to can be represented by either != or &lt;&gt;. Two values of different types are converted to the same type before comparison- integers upgrade to floating-point, and numbers upgrade to strings. You cannot compare arrays or hashes. Objects of the same type can only be compared using equal-to and not-equal-to. String comparison is not case-sensitive.
  186. 'is': 'is' compares data types. The left operand is always a value, but the second may be a value or an actual data type such as 'int'. If the data types match exactly, the result is true (1) otherwise the result is false (0). No conversion is done on either value before comparing. 'var' types are not considered distinct data types for this purpose- the actual underlying type contained within the variant is used.
  187. 'and', 'or', 'xor': Logical operators return the logical 'and', 'or', or 'xor' of two boolean values, returning true (1) or false (0).
  188. '=': Assignment = is used to assign a new value to an existing variable or array or hash subscript. Although often used as a command on it's own line, assignment may also be used within a larger expression, and returns the variable being assigned to. Multiple assignments in a row are evaluated from right-to-left- 'a = b = c = 3' will assign 3 to all three variables. The left operand to = must always be a variable or array or hash subscript into a variable.
  189. Control Structures
  190. end, return
  191. 'end' very simply ends the script entirely. 'return' is more friendly when used in a function, returning to the previous place within the script's code. (see Functions for using 'return' with a return type, and see 'reply' for another similar keyword)
  192. if, else
  193. <PRE>if (a == 5) {
  194. doFive()
  195. }
  196. else if (a == 10) {
  197. doTen()
  198. }
  199. else {
  200. doOther()
  201. }</PRE>
  202. 'if' must be followed by an expression, optionally in parenthesis, then a command or block of code. This code is executed if and only if the expression is true. This may then be folowed by 'else' and another command or block of code that will be executed if and only if the expression is false. To chain multiple conditions, simply write 'else if'.
  203. repeat
  204. <PRE>repeat {
  205. if (leftPressed()) doStuff()
  206. }</PRE>
  207. 'repeat' is used to create an infinite loop- the command or block of code after 'repeat' will be repeated indefinately until stopped from within or by an outside force. This is designed to be used for "main" or "idle" loops that run every game cycle.
  208. This loop specifically differs from a manual infinite loop (such as 'while (1) { }') because it gaurantees that the loop will only run a single time each game cycle, even if the programmer does not insert their own delay. If you use other looping methods, you should be careful to ensure that your code includes a voluntary delay if you wish the loop to only execute once per cycle.
  209. for
  210. <PRE>for (i = 1 to 10) {
  211. output(i)
  212. }</PRE>
  213. 'for' creates a simple counting loop. 'for' is followed by a special assignment optionally enclosed in parenthesis, then a command or block of code. This assignment must consist of a variable identifier, '=', a starting value, 'to', and an ending value. No other syntax is allowed, and the variable must already exist.
  214. The variable will be initialized to the starting value, the loop will execture, the variable will increase or decrease by one, and then if the variable has not yet passed the ending value, the loop will repeat. In the given example, i will be equal to 11 when the loop exits. The starting and ending values are stored when the loop starts and will not be recalculated during each loop. A 'for' loop can only increase or decrease by one. Other more complex loops must be made using 'do', 'while', and 'until'.
  215. foreach
  216. <PRE>foreach (i in iArray) {
  217. output(i)
  218. }</PRE>
  219. 'foreach' loops through all values in an array or hash. 'foreach' is followed by a special expression optionally enclosed in parenthesis, then a command or block of code. This expression must consist of a variable identifier, 'in', and an expression that evaluates to a hash or array. No other syntax is allowed, and the variable must already exist.
  220. For an array, the variable will be assigned each value within the array in turn, starting with the first value. The loop will repeat for each possible value. Modifying the variable within the loop will modify the original array element. Modifying the array directly from within the loop will result in indeterminate behavior.
  221. For a hash, the variable will be assigned the string indexing each existing hash entry in turn, in arbitrary order. The loop will repeat for each possible value. Modifying the variable within the loop will have no effect. Modifying the hash directly from within the loop will result in indeterminate behavior.
  222. do, while, until
  223. <PRE>do {
  224. someStuff()
  225. } while (a &lt; 10)</PRE>
  226. These keywords allow you to construct loops that loop based on your own conditions. These loops can be constructed in one of two ways. Prefix loops are constructed with the 'while' or 'until' keyword, then a condition optionally in parenthesis, then a statement or block of code. Postfix loops are constructed with the 'do' keyword, then a statement or block of code, then the 'while' or 'until' keyword, then a condition optionally in parenthesis.
  227. A prefix loop checks the condition, then executes the code, then repeats. A postfix loop executes the code, then checks the condition, then repeats. A 'while' loop repeats as long as the condition is true. An 'until' loop repeats as long as the condition is false.
  228. break, continue, restart
  229. These keywords are used for specialized loop control, and can appear in any for, foreach, do, while, until, or repeat loop.
  230. 'break' immediately exits the current loop, resuming execution at the code immediately following.
  231. 'continue' immediately jumps to the end of the loop but does not exit. Any loop variable (for and foreach loops) is updated, and the loop condition is then checked as normal and the loop may or may not repeat.
  232. 'restart' immediately jumps to the beginning of the loop. No condition is checked and no loop variable is updated. 'continue' and 'restart' are functionally identical for a repeat or infinite loop.
  233. All three keywords may optionally be followed by a loop name. Loops may be named by placing a label immediately preceding the keyword that starts the loop. A label consists of an identifier, followed by a colon. When using a keyword with a label, it causes the keyword to refer to a specific loop, instead of the innermost loop. Labels must, of course, refer to loops you are currently within. An example-
  234. <PRE>outer:
  235. while (y &lt; 100) {
  236. x = 0
  237. while (x &lt; 100) {
  238. x = x + 1
  239. if (check(x, y)) break outer
  240. }
  241. y = y + 1
  242. }</PRE>
  243. Objects
  244. Objects represent game resources and elements, including sprites and entities. Object references can be stored in variables of the appropriate type, and then acted upon by accessing methods and properties of those objects. Methods are conceptually interchangable with functions, and properties with variables, except that methods and properties are part of the object being referenced, instead of the current script/entity.
  245. All built-in object data types begin with obj_, such as obj_sprite or obj_entity. You can also use script names as custom object types- this is called a "script type". When doing so, you must precede them with obj_entity or another data type to clarify they are meant as an object type. You can, alternatively, enclose a script type in 'single quotes' to clarify it is meant as a data type. This also allows you to use non-identifier-friendly script names.
  246. At the simplest level, you access methods and properties of an object using the . dot operator. The left side of the . is the object reference, and the right side of the . is the identifier that matches the method or property you wish to access. This construct can then be used anywhere a variable (if a property) or a function (if a method) would be used, and in exactly the same way-
  247. <PRE>mySprite.x = 10
  248. enemyEntity.doStuff("flag")</PRE>
  249. All built-in object types (other than obj_entity) have a predefined, built-in set of methods and properties that you are limited to using. obj_entity types as well as script types allow you to access any method or property you wish to define- however, if the method or property does not exist within the referenced entity, nothing happens and/or an empty value results.
  250. For built-in object types, this should be sufficient to cover all cases (except for 'with', described later), but entities have several additional layers of complexity.
  251. Matching Entities
  252. Instead of using a variable or other reference to a specific entity, you can also use a string on the left side of the dot operator. This is then treated as an entity NAME. (which can be different from the name or type of script the entity is running) If calling a method, that method is called for all matching entities; if setting a property, all matching entities are modified. If reading a property, however, only one matching entity can be read (chosen arbitrarily), and of course if no matching entities are found, an empty value is used.
  253. The string may also contain * as a wildcard, to match zero or more of any characters, in order to perform more specialized searches. Also, note that this search is not case-sensitive. Also note that the search may match the current entity.
  254. You may also use 'as' to restrict the script type of matched entities. For example, '"bob" as Enemy' will match all entities named "bob", but only if they are of the 'Enemy' script type. (i.e. running the script named 'Enemy'.)
  255. You cannot assign a string to an obj_entity variable or a script type variable- you can only use them in this way with the dot operator. Of course, you can simply store the string in a string variable and use that variable with the dot operator if desired.
  256. Be careful with this syntax- it is intended for simple operations and does not work as may be expected on more complex ones. For example, '"enemy".x = "enemy".x + 1' will not properly increase the x property of every matching entity- it will read x from a single entity, and then assign that value (plus one) to every matching entity. Similar cavaets apply to expressions such as '"enemy".go("enemy".direction)'. For these types of expressions, you should use the 'with' construct.
  257. Note that this syntax can only find entities within the current scene or within global entities- entities on other scenes are "hidden" from this search.
  258. Also note that if an identifier is used on the left side of . that is unrecognized, the compiler can automatically treat it as a string to match in this way. This means that, if there is no identifier named bob, then bob.x() is treated as "bob".x() by the compiler. This is an optional feature, and may or may not default to on.
  259. Entity Properties
  260. Accessing entity properties is simple- simply follow an entity reference (or string) with a dot, and the name of a property. If the property exists, you will be able to read from or assign to it. If it does not, then reading it will produce an empty value, and assigning to it will do nothing.
  261. Entity Methods
  262. Accessing entity methods is similar- follow an entity reference (or string) with a dot, and the name of a method, optionally followed by any parameters. If the compiler can't determine whether you mean to call a method or access a property based on your syntax and/or the type of object, it will assume you mean to access a property. You can simply add an empty parameter list () to clarify this situation if needed.
  263. If a noted method does not exist, then nothing happens. If you use the return value of a non-existant method, an empty value will be used. If the noted method does exist, but you provide the wrong number or type of parameters, all possible conversions will be made, and any missing values will use empty values.
  264. Special Entities
  265. You can use the keyword 'all' to access all entities on the current scene and all global entities. You may restrict this with 'as' just as you would restrict string-based searches. 'all' is exactly equivalent to using "*" to search for all entities, but is cleaner and more optimized. 'all' may not be stored in a variable. 'all' also includes the current entity- use 'all_other' to access every entity except the current.
  266. You can use the keyword 'this' as a reference to the current entity. 'this' may be assigned to a variable.
  267. The keyword 'nothing' refers to no entity, and can be assigned to any obj_entity or script type variable. Using 'nothing' with the dot operator is technically valid syntax, but will have no useful effect.
  268. Optimizing Entity Access
  269. The compiler can optimize your access to entity methods and properties better if it knows what type of entity you are accessing. For this reason, using specific script types for variables is better than using obj_entity variables when you know you will only be storing references to one script type. When that is not feasible, using 'as' is almost as efficient-
  270. <PRE>obj_entity myEntity = findEntity("bob")
  271. (myEntity as bobScript).doStuff()</PRE>
  272. Of course, if you specify the incorrect type with 'as', then the method or property access will have no effect, as the process of attempting to change the entity reference to the more specific type will turn it into an empty reference. ('nothing')
  273. Return Values from Entity Methods
  274. When you access another entity's methods as a standalone command outside of an expression, both entities are free to continue running their script separately. Neither script will require the other to finish in order to continue. (however, see Concurrency for more details on exactly how this works)
  275. However, if you use an entity's method within an expression, the original script will halt at that point and wait for the requested entity to finish the entire method and return a value, so that the value can be used in the expression. This may cause a delay if the requested entity's method takes more than one game cycle to run. Improper coding may even lead the original script waiting indefinately for a value that never comes.
  276. Also note that if you use 'all' or a string to call a method within an expression, no more than one matching entity will have it's method called. Any remaining matches will be ignored.
  277. The entity method can use the 'reply' keyword at any time to return a value without actually halting. 'reply' is exactly the same as 'return', except it does not exit the function or method, but instead continues onward; it does, however, return a value to the original waiting script.
  278. Using 'reply' in a function that was not called as a method has no effect. 'reply' must be used from within the original method function- you cannot chain to another function and have that function use 'reply' to any effect.
  279. Concurrency
  280. (copy from outline)