Difference between revisions of "Array Reference"
imported>Egocarib |
imported>Egocarib (added new warning about problem with array.find()) |
||
Line 16: | Line 16: | ||
MyObject[] | MyObject[] | ||
</source> | </source> | ||
<br> | |||
== Array Creation == | == Array Creation == | ||
<array creation> ::= 'new' <element type> '[' <int> ']' | <array creation> ::= 'new' <element type> '[' <int> ']' | ||
Line 38: | Line 38: | ||
MyScript[] x = new MyScript[5] | MyScript[] x = new MyScript[5] | ||
</source> | </source> | ||
<br> | |||
== Array Length == | == Array Length == | ||
<array length> ::= <expression> '.' 'Length' | <array length> ::= <expression> '.' 'Length' | ||
Line 56: | Line 56: | ||
x = myArray.Length ; x will get 0 | x = myArray.Length ; x will get 0 | ||
</source> | </source> | ||
<br> | |||
== Array Elements == | == Array Elements == | ||
<array access> ::= <expression> '[' <expression> ']' | <array access> ::= <expression> '[' <expression> ']' | ||
To get a specific array element, just put the index of the element you want between two square brackets. This value can also come from an [[Expression Reference|expression]], or a return value of a [[Function Reference|function]], or something else. The valid elements in an array are from 0 to the length minus 1. | To get a specific array element, just put the index of the element you want between two square brackets. This value can also come from an [[Expression Reference|expression]], or a return value of a [[Function Reference|function]], or something else (see Warnings below for an exception). The valid elements in an array are from 0 to the length minus 1. | ||
=== Examples === | === Examples === | ||
Line 72: | Line 72: | ||
x = myArray[myArray.Length - 1] | x = myArray[myArray.Length - 1] | ||
</source> | </source> | ||
<br> | |||
== Warnings == | == Warnings == | ||
Only the = operator can be used with an [[Arrays_(Papyrus)|array]]: | *Only the = 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. | ||
myArray[3] += 5 | myArray[3] += 5 | ||
</source> | </source> | ||
( | <br> | ||
*The array Find() function cannot be used within an array's element index brackets | |||
<source lang="papyrus"> | |||
;this works fine: | |||
i = myArray.Find(none) ;(find the first blank element) | |||
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: | |||
myArray[myArray.Find(none)] = newValue | |||
</source> | |||
<br> | |||
[[Category:Scripting]] | [[Category:Scripting]] | ||
[[Category:Papyrus]] | [[Category:Papyrus]] | ||
[[Category:Papyrus Language Reference]] | [[Category:Papyrus Language Reference]] |
Revision as of 19:27, 1 September 2013
Arrays are values that contain several other values, indexed by a 0-based number. (0 is the first element, 1 in the second, and so on).
Array Type
<array type> ::= <element type> '[' ']'
An array type is denoted by adding a pair of empty brackets after the type of the elements. The element type may only be a non-array type, so multi-dimentional arrays are not allowed. These types can be used anywhere else other types are used - as parameter, return, variable, and property types.
Examples
; An array of integers
int[]
; An array of MyObjects
MyObject[]
Array Creation
<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)
The initial value of each element will be the default value for the type.
If you make an array property, then the Creation Kit will determine the size of the array by how many elements are put into it.
Note that this cannot appear in the script outside of a function.
Examples
; Create an array of 20 floats
float[] x = new float[20]
; Create an array of 5 MyScripts
MyScript[] x = new MyScript[5]
Array Length
<array length> ::= <expression> '.' 'Length'
To get the length of an array, you can access the read-only length property on the variable holding the array. If the variable is None, the length will still succeed, but return 0. Note that the last valid index in an array is the length of the array, minus 1.
Examples
; Get the length of our array
int[] myArray = new int[10]
x = myArray.Length ; x will get 10
; Get the length of an uninitialized array (None)
float[] myArray
x = myArray.Length ; x will get 0
Array Elements
<array access> ::= <expression> '[' <expression> ']'
To get a specific array element, just put the index of the element you want between two square brackets. This value can also come from an expression, or a return value of a function, or something else (see Warnings below for an exception). The valid elements in an array are from 0 to the length minus 1.
Examples
; get the first element in the array
x = myArray[0]
; get the last element of the array
x = myArray[myArray.Length - 1]
Warnings
- Only the = operator can be used with an array (see Assignment Operators):
; Will not compile, as the compiler doesn't know how to handle it.
myArray[3] += 5
- The array Find() function cannot be used within an array's element index brackets
;this works fine:
i = myArray.Find(none) ;(find the first blank element)
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:
myArray[myArray.Find(none)] = newValue