Difference between revisions of "GetAt - FormList"
Jump to navigation
Jump to search
imported>Rhavlovick m (1 revision: Clobber re-import by Henning) |
imported>DavidJCobb (→Notes: created refs aren't made persistent by membership in form lists!) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 18: | Line 18: | ||
== Examples == | == Examples == | ||
<source lang="papyrus"> | *Print out the forms in the list | ||
<source lang="papyrus">Int iIndex = kFormList.GetSize() | |||
FormList | While iIndex > 0 | ||
iIndex -= 1 | |||
Debug.Trace("Form " + iIndex + " is " + kFormList.GetAt(iIndex)) | |||
EndWhile</source> | |||
EndWhile | *Get the index of member in list. Return -1 if member is not found. | ||
</source> | <source lang="papyrus">Int Function iGetFormIndex(FormList akList, Form akMember) Global | ||
If akList.HasForm(akMember) | |||
Int iIndex = akList.GetSize() ; Will always return a finite value | |||
While iIndex > 0 | |||
iIndex -= 1 | |||
If akList.GetAt(iIndex) == akMember | |||
Return iIndex | |||
EndIf | |||
EndWhile | |||
EndIf | |||
Return -1 ; Either the form is not in the list, is not currently loaded if non-persistent | |||
EndFunction</source> | |||
* Search a LONG FormList of ORDERED FORMS (by FormID) and return. -1 if not found. Binary Search Method adapted to Papyrus from Wikipedia's [http://en.wikipedia.org/wiki/Binary_search_algorithm#Deferred_detection_of_equality|deferred detection of equality] example. | |||
<source lang="papyrus">Int Function GetFormIndexOrdered(FormList akList, Form akMember) Global | |||
Int iFormID = akMember.GetFormID() | |||
Int iMin = 0 | |||
Int iMax = akList.GetSize() - 1 | |||
While iMax > iMin | |||
Int iMid = (iMin + iMax) / 2 | |||
If iFormID > akList.GetAt(iMid).GetFormID() | |||
iMin = iMid + 1 | |||
Else | |||
iMax = iMid | |||
EndIf | |||
EndWhile | |||
If (iMax == iMin) && (iFormID == akList.GetAt(iMin).GetFormID()) | |||
Return iMin | |||
Else | |||
Return -1 | |||
EndIf | |||
EndFunction</source> | |||
== Notes == | |||
* Keep in mind that if you don't need the exact index of a formlist entry, the native [[HasForm_-_FormList | HasForm()]] will always be the quickest method to find out whether the list contains an entry--far quicker even than a binary search like the one above. | |||
* A created reference does not become persistent by virtue of being added to a FormList. If you try to retrieve the reference from the FormList when it is not loaded and not persistent, you will get an incorrect result or no result. | |||
== See Also == | == See Also == | ||
*[[FormList Script]] | *[[FormList Script]] | ||
*[[GetSize - FormList]] | *[[GetSize - FormList]] | ||
*[[Complete_Example_Scripts#Cycle_Through_an_Array_of_Objects_and_Perform_an_Action_on_Each_Object.28FormLists.29 | FormList Script Example: Disabling/enabling a large quantity of objects easily]] |
Latest revision as of 02:46, 4 October 2016
Member of: FormList Script
Returns the form at a specified index in the list.
Syntax[edit | edit source]
Form Function GetAt(int aiIndex) native
Parameters[edit | edit source]
- aiIndex: The index in the list we want to fetch the form from
- The index is 0-based. If a list has 3 items in it, valid indices are: 0, 1 and 2
Return Value[edit | edit source]
Returns the form at index aiIndex, none in case of error (such as wrong index value)
Examples[edit | edit source]
- Print out the forms in the list
Int iIndex = kFormList.GetSize()
While iIndex > 0
iIndex -= 1
Debug.Trace("Form " + iIndex + " is " + kFormList.GetAt(iIndex))
EndWhile
- Get the index of member in list. Return -1 if member is not found.
Int Function iGetFormIndex(FormList akList, Form akMember) Global
If akList.HasForm(akMember)
Int iIndex = akList.GetSize() ; Will always return a finite value
While iIndex > 0
iIndex -= 1
If akList.GetAt(iIndex) == akMember
Return iIndex
EndIf
EndWhile
EndIf
Return -1 ; Either the form is not in the list, is not currently loaded if non-persistent
EndFunction
- Search a LONG FormList of ORDERED FORMS (by FormID) and return. -1 if not found. Binary Search Method adapted to Papyrus from Wikipedia's detection of equality example.
Int Function GetFormIndexOrdered(FormList akList, Form akMember) Global
Int iFormID = akMember.GetFormID()
Int iMin = 0
Int iMax = akList.GetSize() - 1
While iMax > iMin
Int iMid = (iMin + iMax) / 2
If iFormID > akList.GetAt(iMid).GetFormID()
iMin = iMid + 1
Else
iMax = iMid
EndIf
EndWhile
If (iMax == iMin) && (iFormID == akList.GetAt(iMin).GetFormID())
Return iMin
Else
Return -1
EndIf
EndFunction
Notes[edit | edit source]
- Keep in mind that if you don't need the exact index of a formlist entry, the native HasForm() will always be the quickest method to find out whether the list contains an entry--far quicker even than a binary search like the one above.
- A created reference does not become persistent by virtue of being added to a FormList. If you try to retrieve the reference from the FormList when it is not loaded and not persistent, you will get an incorrect result or no result.