Difference between revisions of "Array Reference"

Jump to navigation Jump to search
997 bytes added ,  08:45, 13 December 2014
m
Noted the restriction that arrays can only be between 1 and 128 in the Array Creation section.
imported>Egocarib
(added new warning about problem with array.find())
imported>Evildavo
m (Noted the restriction that arrays can only be between 1 and 128 in the Array Creation section.)
Line 20: Line 20:
  <array creation> ::= 'new' <element type> '[' <int> ']'
  <array creation> ::= 'new' <element type> '[' <int> ']'


To create an array, use the "new" keyword, followed by the type and size. The Size is denoted by the integer between the two square brackets, and cannot be a variable. (In other words, array size must be defined at compile time)  
To create an array, use the "new" keyword, followed by the type and size. The Size is denoted by the integer between the two square brackets, which must be between 1 and 128. Also, it cannot be a variable. (In other words, array size must be defined at compile time)  


The initial value of each element will be the [[Default Value Reference|default value]] for the type.
The initial value of each element will be the [[Default Value Reference|default value]] for the type.
Line 74: Line 74:
<br>
<br>
== Warnings ==
== Warnings ==
*Only the = operator can be used with an [[Arrays_(Papyrus)|array]] (see [[Operator_Reference#Assignment_Operators|Assignment Operators]]):
*Only the = assignment operator can be used with an [[Arrays_(Papyrus)|array]] (see [[Operator_Reference#Assignment_Operators|Assignment Operators]]):
<source lang="papyrus">
<source lang="papyrus">
  ; Will not compile, as the compiler doesn't know how to handle it.
  ; Will not compile, as the compiler doesn't know how to handle it.
Line 80: Line 80:
</source>
</source>
<br>
<br>
*The array Find() function cannot be used within an array's element index brackets
=== Compiler Issues ===
Several compiler errors have been identified that are caused by using expressions inside of array index brackets. The standard papyrus compiler sometimes compiles expressions inside of array index brackets incorrectly, leading to unexpected behavior and data corruption. As a result, it is recommended to avoid using complex expressions or function calls inside of array index brackets when possible. '''[http://forums.bethsoft.com/topic/1491368-weird-array-index-behavior/ See this discussion]''' for further details and analysis of the problems outlined below:
 
<br>
*Using complex expressions inside of the index brackets of an integer array can cause unexpected behavior -
<source lang="papyrus">
;these work fine:
myIntArray[i] = newValue
myIntArray[i * 5] = newValue
 
;this will result in unexpected values being inserted into the array at run time:
myIntArray[i * 5 + 1] = newValue
</source>
<br>
*The array Find() function should not be used within an array's index brackets; it can also cause unexpected results -
<source lang="papyrus">
<source lang="papyrus">
;this works fine:
;this works fine:
Line 86: Line 100:
myArray[i] = newValue  ;(and fill it with our newValue)
myArray[i] = newValue  ;(and fill it with our newValue)


;this will compile, but it will not work correctly in the script, and the value will not be filled as expected:
;this will result in values being inserted into unpredictable indices of the array at run time:
myArray[myArray.Find(none)] = newValue
myArray[myArray.Find(none)] = newValue
</source>
</source>
Anonymous user

Navigation menu