Array Reference: Difference between revisions

Jump to navigation Jump to search
m
Convert source tags to syntaxhighlight
m (→‎Large Arrays without SKSE: Reverted accidental edit of heading level)
m (Convert source tags to syntaxhighlight)
 
Line 7: Line 7:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; An array of integers
; An array of integers
int[]
int[]
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; An array of MyObjects
; An array of MyObjects
MyObject[]
MyObject[]
</source>
</syntaxhighlight>
<br>
<br>
== Array Creation ==
== Array Creation ==
Line 29: Line 29:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Create an array of 20 floats
; Create an array of 20 floats
float[] x = new float[20]
float[] x = new float[20]
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Create an array of 5 MyScripts
; Create an array of 5 MyScripts
MyScript[] x = new MyScript[5]
MyScript[] x = new MyScript[5]
</source>
</syntaxhighlight>
<br>
<br>
== Array Length ==
== Array Length ==
Line 45: Line 45:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Get the length of our array
; Get the length of our array
int[] myArray = new int[10]
int[] myArray = new int[10]
x = myArray.Length ; x will get 10
x = myArray.Length ; x will get 10
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Get the length of an uninitialized array (None)
; Get the length of an uninitialized array (None)
float[] myArray
float[] myArray
x = myArray.Length ; x will get 0
x = myArray.Length ; x will get 0
</source>
</syntaxhighlight>
<br>
<br>
== Array Elements ==
== Array Elements ==
Line 63: Line 63:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; get the first element in the array
; get the first element in the array
x = myArray[0]
x = myArray[0]
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; get the last element of the array
; get the last element of the array
x = myArray[myArray.Length - 1]
x = myArray[myArray.Length - 1]
</source>
</syntaxhighlight>
<br>
<br>
== Warnings ==
== Warnings ==
*Only the = assignment 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">
<syntaxhighlight 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>
</syntaxhighlight>
<br>
<br>
=== Compiler Issues ===
=== Compiler Issues ===
Line 85: Line 85:
<br>
<br>
*Using complex expressions inside of the index brackets of an integer array can cause unexpected behavior -
*Using complex expressions inside of the index brackets of an integer array can cause unexpected behavior -
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
;these work fine:
;these work fine:
myIntArray[i] = newValue
myIntArray[i] = newValue
Line 92: Line 92:
;this will result in unexpected values being inserted into the array at run time:
;this will result in unexpected values being inserted into the array at run time:
myIntArray[i * 5 + 1] = newValue
myIntArray[i * 5 + 1] = newValue
</source>
</syntaxhighlight>
<br>
<br>
*The array Find() function should not be used within an array's index brackets; it can also cause unexpected results -
*The array Find() function should not be used within an array's index brackets; it can also cause unexpected results -
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
;this works fine:
;this works fine:
i = myArray.Find(none)  ;(find the first blank element)
i = myArray.Find(none)  ;(find the first blank element)
Line 102: Line 102:
;this will result in values being inserted into unpredictable indices of the array at run time:
;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>
</syntaxhighlight>
<br>
<br>
== Advanced Usage ==
== Advanced Usage ==
Line 108: Line 108:
You can create Arrays larger than 128 elements and variable in size using the helper functions in the [[Utility_Script#SKSE_Global_Functions|Utility script]].
You can create Arrays larger than 128 elements and variable in size using the helper functions in the [[Utility_Script#SKSE_Global_Functions|Utility script]].


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
float[] LongArrayOfFloats = Utility.CreateFloatArray(512)
float[] LongArrayOfFloats = Utility.CreateFloatArray(512)
int i = 2048
int i = 2048
string[] VariableSizeArrayOfStrings = Utility.CreateStringArray(i)
string[] VariableSizeArrayOfStrings = Utility.CreateStringArray(i)
</source>
</syntaxhighlight>


You can access it like any other array.
You can access it like any other array.


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
Debug.trace(LongArrayOfFloats[250])
Debug.trace(LongArrayOfFloats[250])
</source>
</syntaxhighlight>


SKSE also allows you to resize any array on the fly:
SKSE also allows you to resize any array on the fly:


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
Utility.ResizeStringArray(VariableSizeArrayOfStrings, 64)
Utility.ResizeStringArray(VariableSizeArrayOfStrings, 64)
</source>
</syntaxhighlight>


=== Large Arrays without SKSE ===
=== Large Arrays without SKSE ===
If an array larger than 128 elements is required, a work-around is to use multiple arrays to form one large array. Below is an example of how this can be accomplished for an array that can hold 512 floats:
If an array larger than 128 elements is required, a work-around is to use multiple arrays to form one large array. Below is an example of how this can be accomplished for an array that can hold 512 floats:


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
float[] Array1
float[] Array1
float[] Array2
float[] Array2
Line 173: Line 173:
endFunction
endFunction


</source>
</syntaxhighlight>
<br>
<br>
[[Category:Scripting]]
[[Category:Scripting]]
[[Category:Papyrus]]
[[Category:Papyrus]]
[[Category:Papyrus Language Reference]]
[[Category:Papyrus Language Reference]]

Navigation menu