Difference between revisions of "ResizeIntArray - Utility"
Jump to navigation
Jump to search
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Scripting]] | [[Category:Scripting]] | ||
[[Category:Papyrus]] | [[Category:Papyrus]] | ||
''' | '''SKSE member of:''' [[Utility Script]] | ||
Resizes an already existing array of Int values to the specified length. <br> | Resizes an already existing array of Int values to the specified length. (This function requires SKSE)<br> | ||
'''Be careful when using this function.''' It is possible to create arrays longer than 128 elements, and size is treated as an unsigned integer. Negative numbers will produce extremely large values, which can cause major issues. | '''Be careful when using this function.''' It is possible to create arrays longer than 128 elements, and size is treated as an unsigned 32-bit integer. Do not exceed a length of 2^31. SKSE's DLL can handle a max length of 2^32 '''however, Papyrus cannot.''' Negative numbers will produce extremely large values, which can cause major issues. | ||
== Syntax == | == Syntax == | ||
Line 14: | Line 14: | ||
* source: an existing array of int values. | * source: an existing array of int values. | ||
* size: new size of the array. | * size: new size of the array. | ||
* fill: a value to fill the array with. | * fill: a value to fill empty elements of the array with, if the array being resized is longer than the source array. | ||
== Return Value == | == Return Value == | ||
An Int array of '''''size''''' length. | An Int array of '''''size''''' length. Element values will match those provided by the source array. If new size (array.length) exceeds size of source array, new elements will will have the value of the fill (if specified) or zero if not. | ||
== Examples == | == Examples == | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; let's make array length equal to available perk points, and fill all elements with 9 | ; let's make array length equal to available perk points, and fill all elements with 9 | ||
; ...this should really be using the "create" function, not "resize" | |||
int[] pointsArray | int[] pointsArray | ||
pointsArray = Utility.ResizeIntArray(pointsArray, Game.GetPerkPoints(), 9) | pointsArray = Utility.ResizeIntArray(pointsArray, Game.GetPerkPoints(), 9) | ||
; Let's resize an existing array, someArray, expanding it by one element. The fill value (in this case, not specified so the default of zero) will be used for new elements (in this case, the last one) and all other element values will remain the same as they were before being resized. | |||
someArray = Utility.ResizeIntArray(someArray, someArray.Length + 1) | |||
; Let's resize an existing array, someArray, by making it one element shorter. The removed element, would be the last one in the array. The data in all other elements should be unchanged. | |||
; NOTE 1: this is not "safe" code, it could theoretically try to Resize the array to zero length (if it's currently 1) which Resize will not allow, resulting in an error in the papyrus log. Use "Create" versions if you need to shrink an existing array to zero elements. | |||
; NOTE 2: The autofill value is meaningless and ignored when shrinking an array. | |||
someArray = Utility.ResizeIntArray(someArray, someArray.Length - 1) | |||
</source> | </source> | ||
== Notes == | == Notes == | ||
* No need to actually create an array with '''''new''''', declaring an array variable is enough. | * No need to actually create an array with '''''new''''', declaring an array variable is enough. However, be sure to "Create" or "Resize" the array before trying to read/set any element values in it, or you will get an error for trying to access an uninitialized array. | ||
== See Also == | == See Also == | ||
*[[Utility Script]] | *[[Utility Script]] |
Latest revision as of 23:40, 3 October 2024
SKSE member of: Utility Script
Resizes an already existing array of Int values to the specified length. (This function requires SKSE)
Be careful when using this function. It is possible to create arrays longer than 128 elements, and size is treated as an unsigned 32-bit integer. Do not exceed a length of 2^31. SKSE's DLL can handle a max length of 2^32 however, Papyrus cannot. Negative numbers will produce extremely large values, which can cause major issues.
Syntax[edit | edit source]
int[] Function ResizeIntArray(int[] source, int size, int fill = 0) global native
Parameters[edit | edit source]
- source: an existing array of int values.
- size: new size of the array.
- fill: a value to fill empty elements of the array with, if the array being resized is longer than the source array.
Return Value[edit | edit source]
An Int array of size length. Element values will match those provided by the source array. If new size (array.length) exceeds size of source array, new elements will will have the value of the fill (if specified) or zero if not.
Examples[edit | edit source]
; let's make array length equal to available perk points, and fill all elements with 9
; ...this should really be using the "create" function, not "resize"
int[] pointsArray
pointsArray = Utility.ResizeIntArray(pointsArray, Game.GetPerkPoints(), 9)
; Let's resize an existing array, someArray, expanding it by one element. The fill value (in this case, not specified so the default of zero) will be used for new elements (in this case, the last one) and all other element values will remain the same as they were before being resized.
someArray = Utility.ResizeIntArray(someArray, someArray.Length + 1)
; Let's resize an existing array, someArray, by making it one element shorter. The removed element, would be the last one in the array. The data in all other elements should be unchanged.
; NOTE 1: this is not "safe" code, it could theoretically try to Resize the array to zero length (if it's currently 1) which Resize will not allow, resulting in an error in the papyrus log. Use "Create" versions if you need to shrink an existing array to zero elements.
; NOTE 2: The autofill value is meaningless and ignored when shrinking an array.
someArray = Utility.ResizeIntArray(someArray, someArray.Length - 1)
Notes[edit | edit source]
- No need to actually create an array with new, declaring an array variable is enough. However, be sure to "Create" or "Resize" the array before trying to read/set any element values in it, or you will get an error for trying to access an uninitialized array.