These example functions are a bit hard-coded to assume that you are using 4 individual arrays for a total of 512 elements. If you want more than that, it can easily be done, you'll just have to rewrite some parts. These might still be a little rough around the edges, so feedback is appreciated. However, they are all tested, and they all work (see test results below). All arrays in these examples store objects of type Form, but this can also be changed to your liking. The idea behind these functions and my general purpose array functions was to make arrays as easy to use as a FormList, which is what the functionality of these functions are tailored around.
Method:
The idea is that you are grouping together several arrays and referring to them in the future by an "Array ID", which is an integer. The first set of functions I defined are my "selector" functions that call the right "Do" functions based on the supplied Array ID. You will notice that the array names are baked into these function calls. I wish it could be made more generic, but since I wanted to keep the function calls short, these functions do have to have prior knowledge of the arrays you intend to use. In these examples, I use 8 arrays broken into 2 Array IDs, 1 and 2. They are: myArray1 through myArray8.
The below functions are what would actually be called by you when trying to interface with your Array IDs.
bool function ArrayAddFormXT(int ArrayID, Form myForm)
Adds a form to the first available element in the first available array associated with this ArrayID.
bool function ArrayRemoveFormXT(int ArrayID, Form myForm, bool bSort = true)
Removes a form from the arrays associated with the ArrayID, and re-sorting the arrays of the Array ID by default.
bool function ArrayHasFormXT(int ArrayID, Form myForm)
Attempts to find the given form in the given Array ID, and returns true if found.
function ArrayClearXT(int ArrayID)
Clears all arrays associated with this array ID.
int function ArrayCountXT(int ArrayID)
Counts the number of indicies associated with this array ID that do not have a "none" type.
int function ArrayCountFormXT(int ArrayID, Form myForm)
Counts the number of times myForm appears in arrays associated with this array ID.
bool function ArraySortXT(int ArrayID)
Removes blank elements by shifting all elements down, moving elements to arrays "below" the current one if necessary.
So, those functions wrap up your logic of calling several different Array IDs and knowing which arrays to pass into the Do functions when called.
Here are the "Do" functions. These perform the actual work by acting on the supplied arrays provided by the functions above. These would not be called directly in your code, unless you really wanted to (though that defeats part of the purpose of abstracting this down to a single Array ID, but it's your choice).
bool function ArrayAddFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm)
Adds a form to the first available element in the first available array.
bool function ArrayRemoveFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm, int ArrayID, bool bSort = false)
Removes a form from the arrays, if found.
bool function ArrayHasFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm)
Attempts to find the given form in the arrays, and returns true if found.
function ArrayClearXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4)
Deletes the contents of arrays.
int function ArrayCountXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4)
Counts the number of indicies in these arrays that do not have a "none" type.
int function ArrayCountFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm)
Attempts to count the number of times the given form appears in the arrays.
bool function ArraySortXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, int i = 0)
Removes blank elements by shifting all elements down, moving elements to arrays "below" the current one if necessary, optionally starting the sort at position i.
Edit: Two more useful functions for getting rid of forms from mods that may no longer be present. See this post.
bool function ArrayRemoveBlankFormsXT(int ArrayID)
Clears all arrays associated with this array ID of blank forms ([Form <None>]) and re-sorts the array ID.
bool function ArrayRemoveBlankFormsXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, int ArrayID)
Clears all arrays of blank forms ([Form <None>]) and re-sorts.
So, that's it. With the above, you can drop your forms into a set of arrays and not worry about managing them yourself, just like it were one big array, with the ease of use of a FormList.
Examples:
ArrayAddFormXT(1, FoodCabbage) ;Adds the form FoodCabbage to the first available element in the arrays associated with ArrayID 1. ArrayRemoveFormXT(2, Gold001) ;Removes the first form matching Gold001 in the arrays associated with ArrayID 1. bool bHasPlayer = ArrayHasFormXT(1, PlayerRef) ;Does Array ID 1 have the form PlayerRef?
So here's the big question. Does it work? The answer, thankfully, is yes. I wrote a test script to exercise most aspects of the above function calls; each test requires the function in question to look ahead to at least the next array, if not all 4 arrays in the Array ID.
Here are the test results. These tests walk all of the arrays in an Array ID (512 elements) several times to verify functionality. It also includes some performance test results, which I was pleasantly surprised and happy with. Trying to do everything that this does using FormLists would take forever, and much of the functionality of FormLists is broken anyway (which is what led me to write my general purpose array functions in the first place). Note: all of the below test results are from the same Papyrus log, but had to be broken up due to length reasons. The entire test took around 4 seconds to run.
Test Array Setup
Test 1 - ArraySortXT Test
Test 2 - ArrayAddFormXT Test
Test 3 - ArrayRemoveFormXT Test
Test 4, 5, 6: ArrayHasFormXT Test, ArrayCountXT Test, ArrayCountFormXT Test
Test 7 - ArrayRemoveFormXT Test (with Sort required)
Test 8 - ArrayClearXT Test
Test 9 - Array Population Test
Highlights from the tests:
1. Everything works. Yay!
2. ArraySortXT sorted 184 elements strung across 4 arrays in 1.533998 seconds. It managed to compact everything into the first 2 arrays after sorting it.
3. ArrayAddFormXT added a new form to the 2nd array of the Array ID in 0.016998 seconds.
4. ArrayRemoveFormXT removed a form at array 2, index 56 in 0.034000 seconds.
5. ArrayHasFormXT found a Gold001 form in array 2 index 12 in 0.034000 seconds.
6. ArrayCountXT counted 184 populated array elements across 4 arrays in 0.015999 seconds.
7. ArrayCountFormXT counted all 4 instances of Gold001 in 0.016998 seconds.
8. ArrayRemoveFormXT removed a Gold001 element from the middle of Array 2 and resorted the array in 0.232998 seconds.
9. ArrayClearXT cleared all 184 array elements by setting them to "none" in 0.015999 seconds.
10. ArrayAddFormXT populated all 512 elements of the Array ID with a FoodCabbage form in 2.250000 seconds, and gracefully returned "false" when all arrays of the Array ID were full.
Enjoy, and let me know if you have any questions or suggestions.
Edited by Chesko, 29 September 2012 - 03:44 PM.
Sign In
Create Account




Back to top

Terms of Service