Difference between revisions of "Array Reference"

Jump to navigation Jump to search
1,508 bytes added ,  10:13, 13 December 2014
Added an advanced example of how to simulate an array larger than 128
imported>Evildavo
m (Noted the restriction that arrays can only be between 1 and 128 in the Array Creation section.)
imported>Evildavo
(Added an advanced example of how to simulate an array larger than 128)
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>
<br>
== Advanced Usage ==
=== Large Arrays ===
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">
float[] Array1
float[] Array2
float[] Array3
float[] Array4
function CreateArray()
{ Use to create the array. }
    Array1 = new float[128]
    Array2 = new float[128]
    Array3 = new float[128]
    Array4 = new float[128]
endFunction
float function GetArrayElement(int index)
{ Use to get the value of the array element at the given index (between 0 and 511). }
    if (index < 128)
        return Array1[index]
    elseIf (index < 256)
        return Array2[index - 128]
    elseIf (index < 384)
        return Array3[index - 256]
    elseIf (index < 512)
        return Array4[index - 384]
    else
        Debug.Trace("Error: Array index " + index + " exceeds the maximum of 511.", 2) ; Error.
    endIf
endFunction
function SetArrayElement(int index, float value)
{ Use to set the value of the array element at the given index (between 0 and 511). }
    if (index < 128)
        Array1[index] = value
    elseIf (index < 256)
        Array2[index - 128] = value
    elseIf (index < 384)
        Array3[index - 256] = value
    elseIf (index < 512)
        Array4[index - 384] = value
    else
        Debug.Trace("Error: Array index " + index + " exceeds the maximum of 511.", 2) ; Error.
    endIf
endFunction
</source>
</source>
<br>
<br>
Anonymous user

Navigation menu