Difference between revisions of "Arrays (Papyrus)"
imported>BarbasTheDog m (Putting a better link) |
imported>BarbasTheDog m (Sorry. The first link was actually better...) |
||
Line 267: | Line 267: | ||
*[http://forums.bethsoft.com/topic/1365378-exampleresourcegeneral-purpose-array-functions/ General Purpose Array Functions] | *[http://forums.bethsoft.com/topic/1365378-exampleresourcegeneral-purpose-array-functions/ General Purpose Array Functions] | ||
*[http:// | *[http://forums.bethsoft.com/topic/1413790-resource-beyond-128-extended-array-functions-to-go-to-512-and-up/ Function library for creating arrays beyond the 128 limit] | ||
==FormLists== | ==FormLists== |
Revision as of 12:46, 2 October 2012
Description
Arrays are special kinds of variables that can hold more then one value of the same type. You select which value you want via a numerical index that ranges from zero to the length of the array, minus one.
For beginners who would like to gain a further understanding of arrays
Although the small description can be used as a limited definition of what an array is, reality is that the concept of an array is a bit deeper, but still very simple to grasp. Many people confuse an array data type (or a "special kind of variable") with an array data structure. Most modern languages implement the latter, not because of performance, but mostly because of the basic idea it stems from. To illustrate, let's say that you want to have a set of 10 weapons. You could do: Weapon weapon1, Weapon weapon2 ... Weapon weapon10. Tedious, is it not? Imagine having to iterate and check every one of those single variables whether or not they meet the requirements of your inquiry. Ugly, yes? Now that you've got a bitter taste in your mouth, let's explore how this is resolved. Notice that they are all variables of type "Weapon", and that they only differ by their content (a value of some sort) and the number which specifies numerically which weapon is which. Now, let's combine that into something useful. We want an array, a set of variables of a specific data type, and we want a way to index them.
A rather simple formal representation:
<identifier> <index_or_count>
That's the gist of it, right? A collection, a structure of data of the same type. An array data structure. Observe an evolution of the upper syntax, C-style: Weapon weapon 10 -> Weapon weapon[10];. Now that the index/count is separated from the identifier, you can access every weapon in the array with your index: weapon[i]. That doesn't seem really that different, what's the benefit of this approach? Ah, now you can use various flow control structures implemented within the language (for, while, do-while loop etc.) to iterate over all the possible indices to compare, change etc. which is impossible with the original approach, since you'd need to construct identifiers at runtime by concatenating the descripting name with the changing index.
Well, that and the mess you'd make. Not to mention other problems, like the lack of virtual memory address adjacency (I'll explain). You see, every variable, function, data structure and pretty much anything you can point at in a programming language has to be addressed in some way. You can't say to the computer: "Yo, get me the value of this variable!". The computer doesn't see variables. Variables of various data types are just human ideas and interpretations of the ones and zeroes running through the veins of your machine. These ones and zeroes (bits -> binary digits) were decided to be resolved into units of eight bits, called bytes. A byte is the smallest addressable construct within a computer's memory and it is this what every variable resolves to (yes, even non-pointer types) in order to read or write a value, a memory address of the variable's first byte. Then, by instructing the computer how large your data type is (eg. 4 bytes) or how precise in terms of bits (eg. 32-bit) it can bring it up and, in short, do awesome things.
Arrays are, memory-wise, just a bunch of bytes stacked together, continuous in memory (virtual memory, that is, physical memory can get messy). From now on, I'll just refer to it as memory. Just remember that the physical layout of memory is not necessarily always continuous. If the system knows the data type of the variable, variable's name or identifier and the desired index, it can calculate the exact memory address of the first byte of the wanted element. How? Well, we mentioned that the byte is the smallest addressable unit. It has an address. Just like you might be living at The Crib of Awesome 44B, New York, a byte of memory gets a number appended to it which the computer uses to track it. And memory is sequential, it's a long street of houses called bytes, to think about it simply.
A data type can be as simple as a byte ( range (2^8)-1 -> 256 - 1 or range [0,255] (unsigned)). Note, a byte is often called a char (short for character), because our symbolic system can be fitted within the boundaries of 256 entries (a simple one at least, there are many ways of representing text on the screen internally). An integer might be precise to a level of 32 bits or even 64 bits. That's 4 bytes and 8 bytes, respectively. So, let's say we are using "classic" 32-bit integers, we want the 6th element of a 10-element array, and we know that the array identifier is anArray.
Remember, anArray points to the memory address of the first byte of the entire array (which is continuous, adjacent elements). And yes, the first byte of the entire array is also the first byte of the first element. We know that each 32-bit integer takes exactly 4 bytes of space. There are 10 elements, therefore, 10 times 4 is 40 bytes. And, by simple logic, 40 memory addresses which are sequential, each one an increment of the former. So if each element takes up exactly 4 bytes of space, which is the first byte of the desired 6th element? Well, 6 times 4 is 24. We want the 24th, 25th, 26th and 27th byte, and today's computer architecture can simply grab it based on the offset (4 bytes), base memory address (resolved from the variable which holds it) and the desired element index.
This is much easier than chasing a fragmented "set" of variables like weapon1, weapon2, weapon3 etc. which could be on "different corners" of the "memory street". And finding an address of the next variable is much easier by adding the offset to the base memory address (first byte of array) than by resolving a variable's memory address for the value and chasing other "elements" around manually. Why? Well, if you'd try offsetting for example weapon1's memory address by 4 (in hopes of accessing weapon2's memory address (first byte)) you could accidentally "lock onto" a memory address that is actually "meant" to be the first and only byte of a char type variable and the system would cast it as an integer. And with it, the 3 bytes further down the memory lane which could "belong" to a completely different variable. Trying to read this variable would yield gibberish and changing it would irreversibly corrupt the memory space of your application with undefined consequences, likely a crash. Now, you should see why it was pointed out that the computer has no idea about puny human data type concepts. It'll do what you tell it to, regardless of whether it makes sense or not.
What you've seen here are just some very basic, coarse ideas behind the concept of arrays, including some of the logic behind them and a very simple introduction to memory at large. This is a very broad topic and you're encouraged to expand upon the knowledge you've just gained and perhaps later return and recognize some of the simplifications employed here.
Declaring Arrays
float[] myFloatArray
ObjectReference[] myObjectArray = new ObjectReference[10]
myFloatArray specifies an empty array of floats. It has a length of 0, and is equal to None. myObjectArray also starts that way, except it is an array of ObjectReference objects. It is then assigned a new array of ObjectReference objects, with 10 elements in it (all of which start with the default value of None). Please note you can only call New within functions (including events) - if you want a variable to start with an empty array, then you can make the array inside the OnInit event.
To create a new array and have it in the "empty" script so all events and functions have access to it you have to (Thanks to DreamKing):
Form[] MyArray
Event OnInit()
MyArray = New Form[20]
EndEvent
Note that you cannot have an array of arrays, or a multi-dimensional array. You may be able to "fake" your needs with multiple While Loops. (Example in Common Tasks.)
You can, of course, make an property that is an array. The editor will show a special UI for it that allows you to add, remove, and re-arrange the array items inside it.
Example:
Weapon[] Property MyWeapons Auto
NB: You cannot use a variable or expression to specify the length of an array - it must be declared with an Integer Literal.
Broken Example:
int NoOfItems = 10
ObjectReference[] MyItems = new ObjectReference[NoOfItems] ; this line does not work
Function Parameters
To accept an array as a parameter to a function, use the same syntax as for declaring an array.
Example:
Function MyArrayFunction(int[] myArray, bool someOtherParameter)
EndFunction
Returning from Function
To return an array from a function, again, use the same syntax.
Example:
int[] Function MyReturnArrayFunction()
int[] someArray = new int[10]
return someArray
endFunction
Creating Arrays
To create an array, use the "new" keyword, followed by the array's element type, and the size of the array in brackets. The array size must be an integer between 1 and 128 - and cannot be a variable. In other words, the size of the array must be set at compile time. Every element in the array will be set to the element's default value, be that 0, false, "", or None.
Example:
new bool[5]
new Weapon[25]
Usually you'll be assigning these directly to new array variables, but if a function wants an array, you can make the new array in the function call, like so:
Example:
MyArrayFunction(new int[20])
Getting/Setting Elements
To get a single value from an array, or to set it, use brackets with the index of the element you want between them after the variable name. The index can be an integer variable, raw integer, or the result of an expression. The range of valid values is from 0 (for the first element) to the length of the array, minus 1.
Example:
myArray[20] = newValue
someRandomValue = myArray[currentIndex]
myArray[i * 2] = newValue
If the array elements are other scripts, you can access properties and functions in the same way.
Example:
DoorArray[currentDoor].Lock()
objectXPos = ObjectArray[currentObject].X
Note that, since arrays are passed and assigned by reference, that any modifications to an array's elements will be reflected in any other variables looking at that array.
Getting Length
You can easily get the length of any array by calling the length property on it. If you assign None to an array, the length will be 0.
Example:
int ArrayLength = myArray.Length
Assigning/Passing Arrays
Assigning one array to another array, or passing an array to a function, is done just like any other variable. However, note that arrays are passed/assigned by reference, just like objects. In other words, if you assign one array to another, both are looking at the same array - and modifications made to one, will be reflected in the other.
Example:
int[] Array1 = new int[5]
int[] Array2 = Array1 ; Array1 and Array2 now look at the same array!
Array1[0] = 10
Debug.Trace(Array2[0]) ; Traces out "10", even though you modified the value on Array1
Once an array is no longer referenced, it is garbage collected (destroyed.)
Example:
int[] Array1 = new int[5]
int[] Array2 = new int[10]
Array2 = Array1 ; Array1 and Array2 now look at the same 5 element array, and Array2's original 10 element array is destroyed.
Casting Arrays
Arrays can only be cast to strings, or bools. If you cast an array to a string, it will put each element in the array inside brackets, seperated by commas. If the array is especially long, it may trim the string a little early and put an ellipsis on the end. If you cast an array to a bool, it will be true if the length is non-zero, and false if the length is zero. You cannot cast an array of one type to an array of a different type, even if the elements would successfully cast.
Example:
Debug.Trace(MyArray) ; Traces out "[element1, element2, element3]" or "[element1, element2, ...]" if the array is too large
if (MyArray)
Debug.Trace("Array has at least one element!")
else
Debug.Trace("Array has no elements!")
endIf
Searching Arrays
(Requires 1.6) Arrays can be searched using two different methods, called "find" and "rfind". Find searches the array from the element you give it (default is element 0) and goes forward through the array to the final element, or the first match it finds, whichever is first. RFind does the same thing, only backwards, starting at the element you give it (default is -1, which means the last element) and going towards element 0. If the item is not found, it will return a negative number for the index.
Find syntax:
int Function Find(;/element type/; akElement, int aiStartIndex = 0) native
RFind syntax:
int Function RFind(;/element type/; akElement, int aiStartIndex = -1) native
Examples:
; Set up an array for the example
string[] myArray = new string[5]
myArray[0] = "Hello"
myArray[1] = "World"
myArray[2] = "Hello"
myArray[3] = "World"
myArray[4] = "Again"
; Prints out "Whee! does not exist in the array"
if myArray.Find("Whee!") < 0
Debug.Trace("Whee! does not exist in the array!")
else
Debug.Trace("Whee! exists in the array!")
endIf
; Prints out "The first Hello is at position 0"
Debug.Trace("The first Hello is at position " + myArray.Find("hello"))
; Prints out "The last Hello is at position 2"
Debug.Trace("The last Hello is at position " + myArray.RFind("hello"))
; Prints out "The first Hello in or after position 2 is at position 2"
Debug.Trace("The first Hello in or after position 2 is at position " + myArray.Find("hello", 2))
Common Tasks
Doing Something to Every Element
Frequently you may want to do something to every element in an array. This is easily done by setting up a while loop with a counter, and then doing something to each element, like so:
Example:
Function DisableAll(ObjectReference[] akObjectArray, Int aiElement = 0)
aiElement = akObjectArray.Length
While aiElement > 0
aiElement -= 1
akObjectArray[aiElement].Disable()
EndWhile
EndFunction
Note that the while loop will go as long as the element is greater than 0. This is because the valid elements are zero to length minus one. If you were to make a while loop using ">=" you would error on the last element, because you would be trying run a non-existent element, -1, through the mill.
Counting Certain Elements
(Requires 1.6) Sometimes you might want to count how many of X is in an array. You can do this with Find and a while loop, making sure that each find starts at one after the location where the previous find located an element. Make sure it is one past the previous location, otherwise you'll get stuck looking at the same one every time.
Example:
; Counts the number of hellos there are in the array
int currentPosition = myArray.Find("hello")
int count = 0
while currentPosition >= 0 ; Loop until we don't find any more (position is less then zero)
count += 1
currentPosition = myArray.Find("hello", currentPosition + 1) ; +1 so we don't find the same one again
endWhile
Debug.Trace("There are " + count + " hellos in the array")
Faking a Multi-Dimensional Array
Assuming you have 4 Int Property arrays with elements 0-9 being 0-9, the example below will print 0000 to 9999 incrementally to the screen.
Int Array1Element = 0
While (Array1Element < Array1.Length)
Int Array2Element = 0
While (Array2Element < Array2.Length)
Int Array3Element = 0
While (Array3Element < Array3.Length)
Int Array4Element = 0
While (Array4Element < Array4.Length)
debug.notification(Array1[Array1Element] + Array2[Array2Element] + Array3[Array3Element] + Array4[Array4Element])
Array4Element += 1
endWhile
Array3Element += 1
endWhile
Array2Element += 1
endWhile
Array1Element += 1
endWhile
Creating a FormID Array
Int variables and properties can be set to hex FormIDs. This can be particularly enabling when using GetFormID, GetForm, and GetFormFromFile. The below will work, but you will find you cannot set an Int[] element as a FormID from within the Creation Kit.
Int iFormID = 0x00000BEE
Game.GetPlayer().PlaceActorAtMe(Game.GetFormFromFile(iFormID, "Killer Bees.ESM") As ActorBase)
The "0x" notation is specific to the Papyrus compiler which internally converts the hex to decimal for you, thus the above works as intended. If, however, you want to store an array of FormIDs with the elements predefined in the Creation Kit, you'll need to first convert them from hex to decimal. Windows' Calculator in "Programmer" mode allows easy conversion. BEE, for instance, is 3054.
Int[] Property iFormIDArray Auto ; Filled with bee actor FormIDs converted from hex to decimal
Game.GetPlayer().PlaceActorAtMe(Game.GetFormFromFile(iFormIDArray[0], "Killer Bees.ESM") As ActorBase)
See Also
FormLists
FormLists can be used to create an array of properties/objects for a script:
FormList Script Example: Disabling/enabling a large quantity of objects easily