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.
Spoiler
bool function ArrayAddFormXT(int ArrayID, Form myForm);-----------\ ;Description \ Author: Chesko ;---------------------------------------------------------------- ;Adds a form to the first available element in the first available array;associated with this ArrayID. ;-------------\ ;Return Values \ ;---------------------------------------------------------------- ; false = Error (array full) OR invalid Array ID ; true = Successif ArrayID == 1bool DoAddForm = ArrayAddFormXT_Do(myArray1, myArray2, myArray3, myArray4, myForm)return DoAddFormelseif ArrayID == 2bool DoAddForm = ArrayAddFormXT_Do(myArray5, myArray6, myArray7, myArray8, myForm)return DoAddFormelsereturn falseendifendFunction
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.
Spoiler
bool function ArrayRemoveFormXT(int ArrayID, Form myForm);-----------\ ;Description \ Author: Chesko ;---------------------------------------------------------------- ;Removes a form from the arrays associated with the ArrayID. ;-------------\ ;Return Values \ ;---------------------------------------------------------------- ; false = Error (form not found) OR invalid Array ID ; true = Successif ArrayID == 1bool DoRemoveForm = ArrayRemoveFormXT_Do(myArray1, myArray2, myArray3, myArray4, myForm, ArrayID, bSort)return DoRemoveFormelseif ArrayID == 2bool DoRemoveForm = ArrayRemoveFormXT_Do(myArray5, myArray6, myArray7, myArray8, myForm, ArrayID, bSort)return DoRemoveFormelsereturn falseendifendFunction
bool function ArrayHasFormXT(int ArrayID, Form myForm)
Attempts to find the given form in the given Array ID, and returns true if found.
Spoiler
bool function ArrayHasFormXT(int ArrayID, Form myForm);-----------\;Description \;----------------------------------------------------------------;Attempts to find the given form in the given Array ID, and returns true if found;-------------\;Return Values \;----------------------------------------------------------------; false = Form not found OR invalid array ID; true = Form foundif ArrayID == 1bool DoHasForm = ArrayHasFormXT_Do(myArray1, myArray2, myArray3, myArray4, myForm)return DoHasFormelseif ArrayID == 2bool DoHasForm = ArrayHasFormXT_Do(myArray5, myArray6, myArray7, myArray8, myForm)return DoHasFormelsereturn falseendifendFunction
function ArrayClearXT(int ArrayID)
Clears all arrays associated with this array ID.
Spoiler
bool function ArrayClearXT(int ArrayID);-----------\;Description \;----------------------------------------------------------------;Clears all arrays associated with this array ID;-------------\;Return Values \;----------------------------------------------------------------; false = Invalid Array ID; true = Complete, Valid Array IDif ArrayID == 1ArrayClearXT_Do(myArray1, myArray2, myArray3, myArray4)return trueelseif ArrayID == 2ArrayClearXT_Do(myArray5, myArray6, myArray7, myArray8)return trueelsereturn falseendifendFunction
int function ArrayCountXT(int ArrayID)
Counts the number of indicies associated with this array ID that do not have a "none" type.
Spoiler
int function ArrayCountXT(int ArrayID);-----------\;Description \;----------------------------------------------------------------;Counts the number of indicies associated with this array ID that do not have a "none" type;-------------\;Return Values \;----------------------------------------------------------------; int DoCount = number of indicies that are not "none"; -1 = Invalid Array IDif ArrayID == 1int DoCount = ArrayCountXT_Do(myArray1, myArray2, myArray3, myArray4)return DoCountelseif ArrayID == 2int DoCount = ArrayCountXT_Do(myArray5, myArray6, myArray7, myArray8)return DoCountelsereturn -1endifendFunction
int function ArrayCountFormXT(int ArrayID, Form myForm)
Counts the number of times myForm appears in arrays associated with this array ID.
Spoiler
int function ArrayCountFormXT(int ArrayID, Form myForm);-----------\;Description \;----------------------------------------------------------------;Counts the number of times myForm appears in arrays associated with this array ID;-------------\;Return Values \;----------------------------------------------------------------; int DoCount = number of times the form appears in the arrays associated with the Array ID; -1 = Invalid Array IDif ArrayID == 1int DoCount = ArrayCountFormXT_Do(myArray1, myArray2, myArray3, myArray4, myForm)return DoCountelseif ArrayID == 2int DoCount = ArrayCountFormXT_Do(myArray5, myArray6, myArray7, myArray8, myForm)return DoCountelsereturn -1endifendFunction
bool function ArraySortXT(int ArrayID)
Removes blank elements by shifting all elements down, moving elements to arrays "below" the current one if necessary.
Spoiler
bool function ArraySortXT(int ArrayID);-----------\;Description \;----------------------------------------------------------------;Removes blank elements by shifting all elements down, moving elements;to arrays "below" the current one if necessary.;-------------\;Return Values \;----------------------------------------------------------------; true = Success; false = Sort not necessaryif ArrayID == 1bool DoSort = ArraySortXT_Do(myArray1, myArray2, myArray3, myArray4)return DoSortelseif ArrayID == 2bool DoSort = ArraySortXT_Do(myArray5, myArray6, myArray7, myArray8)return DoSortelsereturn falseendifendFunction
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.
Spoiler
bool function ArrayAddFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm);-----------\ ;Description \ Author: Chesko ;---------------------------------------------------------------- ;Adds a form to the first available element in the first available array.;-------------\ ;Return Values \ ;---------------------------------------------------------------- ; false = Error (array full) ; true = Successint i = 0;notification("myArray.Length = " + myArray.Length) while i < fArray1.Length if fArray1[i] == none fArray1[i] = myForm ;notification("Adding " + myForm + " to the array.") return true else i += 1 endif endWhilei = 0while i < fArray2.Length if fArray2[i] == none fArray2[i] = myForm ;notification("Adding " + myForm + " to the array.") return true else i += 1 endif endWhilei = 0while i < fArray3.Length if fArray3[i] == none fArray3[i] = myForm ;notification("Adding " + myForm + " to the array.") return true else i += 1 endif endWhilei = 0while i < fArray4.Length if fArray4[i] == none fArray4[i] = myForm ;notification("Adding " + myForm + " to the array.") return true else i += 1 endif endWhile return false ;All arrays are fullendFunction
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.
Spoiler
bool function ArrayRemoveFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm, int ArrayID, bool bSort = true);-----------\;Description \;----------------------------------------------------------------;Removes a form from the arrays, if found.;-------------\;Return Values \;----------------------------------------------------------------; false = Error (Form not found); true = Successint i = 0while i < fArray1.Lengthif fArray1[i] == myFormfArray1[i] = none;notification("Removing element " + i)if bSort == trueArraySortXT(ArrayID)endifreturn trueelsei += 1endifendWhilei = 0while i < fArray2.Lengthif fArray2[i] == myFormfArray2[i] = none;notification("Removing element " + i)if bSort == trueArraySortXT(ArrayID)endifreturn trueelsei += 1endifendWhilei = 0while i < fArray3.Lengthif fArray3[i] == myFormfArray3[i] = none;notification("Removing element " + i)if bSort == trueArraySortXT(ArrayID)endifreturn trueelsei += 1endifendWhilei = 0while i < fArray4.Lengthif fArray4[i] == myFormfArray4[i] = none;notification("Removing element " + i)if bSort == trueArraySortXT(ArrayID)endifreturn trueelsei += 1endifendWhilereturn falseendFunction
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.
Spoiler
bool function ArrayHasFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm);-----------\;Description \;----------------------------------------------------------------;Attempts to find the given form in the arrays, and returns true if found.;-------------\;Return Values \;----------------------------------------------------------------; false = Form not found; true = Form foundint i = 0while i < fArray1.Lengthif fArray1[i] == myFormreturn trueelsei += 1endifendWhilei = 0while i < fArray2.Lengthif fArray2[i] == myFormreturn trueelsei += 1endifendWhilei = 0while i < fArray3.Lengthif fArray3[i] == myFormreturn trueelsei += 1endifendWhilei = 0while i < fArray4.Lengthif fArray4[i] == myFormreturn trueelsei += 1endifendWhilereturn falseendFunction
function ArrayClearXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4)
Deletes the contents of arrays.
Spoiler
function ArrayClearXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4);-----------\;Description \;----------------------------------------------------------------;Deletes the contents of arrays.;-------------\;Return Values \;----------------------------------------------------------------; noneint i = 0while i < fArray1.LengthfArray1[i] = nonei += 1endWhilei = 0while i < fArray2.LengthfArray2[i] = nonei += 1endWhilei = 0while i < fArray3.LengthfArray3[i] = nonei += 1endWhilei = 0while i < fArray4.LengthfArray4[i] = nonei += 1endWhileendFunction
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.
Spoiler
int function ArrayCountXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4);-----------\;Description \;----------------------------------------------------------------;Counts the number of indicies associated with this array ID that do not have a "none" type.;-------------\;Return Values \;----------------------------------------------------------------; int myCount = number of indicies that are not "none"int myCount = 0int i = 0while i < fArray1.Lengthif fArray1[i] != nonemyCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray2.Lengthif fArray2[i] != nonemyCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray3.Lengthif fArray3[i] != nonemyCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray4.Lengthif fArray4[i] != nonemyCount += 1i += 1elsei += 1endifendWhile;notification("MyCount = " + myCount)return myCountendFunction
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.
Spoiler
int function ArrayCountFormXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, Form myForm);-----------\;Description \;----------------------------------------------------------------;Attempts to count the number of times the given form appears in the arrays.;-------------\;Return Values \;----------------------------------------------------------------; 0 = Form not found; int i = Number of times form appears in arrayint iCount = 0int i = 0while i < fArray1.Lengthif fArray1[i] == myFormiCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray2.Lengthif fArray2[i] == myFormiCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray3.Lengthif fArray3[i] == myFormiCount += 1i += 1elsei += 1endifendWhilei = 0while i < fArray4.Lengthif fArray4[i] == myFormiCount += 1i += 1elsei += 1endifendWhilereturn iCountendFunction
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.
Spoiler
bool function ArraySortXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, int i = 0);-----------\;Description \ Author: Chesko;----------------------------------------------------------------;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.;Optionally starts sorting from element i.;-------------\;Return Values \;----------------------------------------------------------------; false = No sorting required; true = Success;notification("Sort Start")bool bFirstNoneFound = falseint iFirstNoneFoundArray = 0int iFirstNonePos = 0while i < 512;Which array am I looking in?int j = 0 ;Actual array index to checkint myCurrArray ;Current arrayif i < 128myCurrArray = 1j = ielseif i < 256 && i >= 128j = i - 128myCurrArray = 2elseif i < 384 && i >= 256j = i - 256myCurrArray = 3elseif i < 512 && i >= 384j = i - 384myCurrArray = 4endifif myCurrArray == 1if fArray1[j] == noneif bFirstNoneFound == falsebFirstNoneFound = trueiFirstNoneFoundArray = myCurrArrayiFirstNonePos = ji += 1elsei += 1endifelseif bFirstNoneFound == true;check to see if it's a couple of blank entries in a rowif !(fArray1[j] == none);notification("Moving element " + i + " to index " + iFirstNonePos)if iFirstNoneFoundArray == 1fArray1[iFirstNonePos] = fArray1[j]fArray1[j] = noneelseif iFirstNoneFoundArray == 2fArray2[iFirstNonePos] = fArray1[j]fArray1[j] = noneelseif iFirstNoneFoundArray == 3fArray3[iFirstNonePos] = fArray1[j]fArray1[j] = noneelseif iFirstNoneFoundArray == 4fArray4[iFirstNonePos] = fArray1[j]fArray1[j] = noneendif;Call this function recursively until it returnsArraySortXT_Do(fArray1, fArray2, fArray3, fArray4, iFirstNonePos + 1)return trueelsei += 1endifelsei += 1endifendifelseif myCurrArray == 2if fArray2[j] == noneif bFirstNoneFound == falsebFirstNoneFound = trueiFirstNoneFoundArray = myCurrArrayiFirstNonePos = ji += 1elsei += 1endifelseif bFirstNoneFound == true;check to see if it's a couple of blank entries in a rowif !(fArray2[j] == none);notification("Moving element " + i + " to index " + iFirstNonePos)if iFirstNoneFoundArray == 1fArray1[iFirstNonePos] = fArray2[j]fArray2[j] = noneelseif iFirstNoneFoundArray == 2fArray2[iFirstNonePos] = fArray2[j]fArray2[j] = noneelseif iFirstNoneFoundArray == 3fArray3[iFirstNonePos] = fArray2[j]fArray2[j] = noneelseif iFirstNoneFoundArray == 4fArray4[iFirstNonePos] = fArray2[j]fArray2[j] = noneendif;Call this function recursively until it returnsArraySortXT_Do(fArray1, fArray2, fArray3, fArray4, iFirstNonePos + 1)return trueelsei += 1endifelsei += 1endifendifelseif myCurrArray == 3if fArray3[j] == noneif bFirstNoneFound == falsebFirstNoneFound = trueiFirstNoneFoundArray = myCurrArrayiFirstNonePos = ji += 1elsei += 1endifelseif bFirstNoneFound == true;check to see if it's a couple of blank entries in a rowif !(fArray3[j] == none);notification("Moving element " + i + " to index " + iFirstNonePos)if iFirstNoneFoundArray == 1fArray1[iFirstNonePos] = fArray3[j]fArray3[j] = noneelseif iFirstNoneFoundArray == 2fArray2[iFirstNonePos] = fArray3[j]fArray3[j] = noneelseif iFirstNoneFoundArray == 3fArray3[iFirstNonePos] = fArray3[j]fArray3[j] = noneelseif iFirstNoneFoundArray == 4fArray4[iFirstNonePos] = fArray3[j]fArray3[j] = noneendif;Call this function recursively until it returnsArraySortXT_Do(fArray1, fArray2, fArray3, fArray4, iFirstNonePos + 1)return trueelsei += 1endifelsei += 1endifendifelseif myCurrArray == 4if fArray4[j] == noneif bFirstNoneFound == falsebFirstNoneFound = trueiFirstNoneFoundArray = myCurrArrayiFirstNonePos = ji += 1elsei += 1endifelseif bFirstNoneFound == true;check to see if it's a couple of blank entries in a rowif !(fArray4[j] == none);notification("Moving element " + i + " to index " + iFirstNonePos)if iFirstNoneFoundArray == 1fArray1[iFirstNonePos] = fArray4[j]fArray4[j] = noneelseif iFirstNoneFoundArray == 2fArray2[iFirstNonePos] = fArray4[j]fArray4[j] = noneelseif iFirstNoneFoundArray == 3fArray3[iFirstNonePos] = fArray4[j]fArray4[j] = noneelseif iFirstNoneFoundArray == 4fArray4[iFirstNonePos] = fArray4[j]fArray4[j] = noneendif;Call this function recursively until it returnsArraySortXT_Do(fArray1, fArray2, fArray3, fArray4, iFirstNonePos + 1)return trueelsei += 1endifelsei += 1endifendifendifendWhilereturn falseendFunction
Edit: Two more useful functions for getting rid of forms from mods that may no longer be present. http://www.gamesas.com/topic/1413790-resource-beyond-128-extended-array-functions-to-go-to-512-and-up/page__view__findpost__p__21596276.
bool function ArrayRemoveBlankFormsXT(int ArrayID)
Clears all arrays associated with this array ID of blank forms ([Form
Spoiler
bool function ArrayRemoveBlankFormsXT(string ArrayID);-----------\;Description \;----------------------------------------------------------------;Clears all arrays associated with this array ID of blank forms ([Form]) and re-sorts the array ID.;-------------\;Return Values \;----------------------------------------------------------------; false = Invalid Array ID; true = Complete, Valid Array IDif ArrayID == 1ArrayRemoveBlankFormsXT_Do(myArray1, myArray2, myArray3, myArray4, ArrayID)return trueelseif ArrayID == 2ArrayRemoveBlankFormsXT_Do(myArray5, myArray6, myArray7, myArray8, ArrayID)return trueelsereturn falseendifendFunction
bool function ArrayRemoveBlankFormsXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, int ArrayID)
Clears all arrays of blank forms ([Form
Spoiler
function ArrayRemoveBlankFormsXT_Do(Form[] fArray1, Form[] fArray2, Form[] fArray3, Form[] fArray4, string ArrayID);-----------\;Description \;----------------------------------------------------------------;Clears all arrays of blank forms ([Form]) and re-sorts.;-------------\;Return Values \;----------------------------------------------------------------; noneint i = 0while i < fArray1.Lengthif fArray1[i] == "[Form ]"fArray1[i] = noneendifi += 1endWhilei = 0while i < fArray2.Lengthif fArray2[i] == "[Form ]"fArray2[i] = noneendifi += 1endWhilei = 0while i < fArray3.Lengthif fArray3[i] == "[Form ]"fArray3[i] = noneendifi += 1endWhilei = 0while i < fArray4.Lengthif fArray4[i] == "[Form ]"fArray4[i] = noneendifi += 1endWhileArraySortXT(ArrayID)endFunction
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 http://www.gamesas.com/topic/1365378-exampleresourcegeneral-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.
http://pastebin.com/EZHx8jvr
http://pastebin.com/7KZc5jaA
http://pastebin.com/b7TuVr9e
http://pastebin.com/xHmzhGRN
http://pastebin.com/JSkuPQnj
http://pastebin.com/98Dc9y1V
http://pastebin.com/ZZCRKuLk
http://pastebin.com/mvSy6X8S
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.