Jump to content

Pushing an entry off an array


Recommended Posts

I want to have a string array of a fixed size, which, when full, can have the oldest entry removed and a new entry added, but not a direct replacement. I want the old entry to drop off the top or bottom of the array, the remaining entries to move along one and the new one to fill the new empty space. I've been looking at papyrus extender and papyrusutil and I don't see a function for that, and I don't know how to build one. This feels a bit 'computer sciency' to me and I never studied that:) so any suggestions?

 

diziet

Link to comment
Share on other sites

As usual, having asked the question, my brain relaxes and I have an idea, but it might not be a good one. Is this all I need?

i = (array.Length) -1
While i > 0
array[i] = array[i-1]
i -= 1
EndWhile
array[i] = new_string

and if so, how cpu intensive would this be on an array with say, 100 elements, 10 elements, 50 elements. Does it also depend on how long the string elements are?

 

diziet

Link to comment
Share on other sites

That should work, the array size will affect performance, but even 100 elements isn't too big a deal.

 

Another option is to use StorageUtil lists.

 

In storageUtil you have StringListShift (removes the first element in the list), or StringListPop (removes the last element in the list)

 

Also with papyrusUtil you can use PushString (adds an element to the end of the array) and SliceStringArray(array, 1) (this would remove the first element of the array)

Link to comment
Share on other sites

Another option, is to not re order your array at all, and keep track of the new array element index with an int. Then you can view the array elements in reverse order like so. I think it would be the most performance friendly.

 

int ArrayIndex = 0 
String[] sArray

Function AddString(String s) ;add a new string to sArray
    sArray[ArrayIndex] = s 
    ArrayIndex += 1 
    If ArrayIndex >= sArray.Length 
        ArrayIndex = 0 
    Endif 
EndFunction 

Function ViewArrayInReverseOrder() 
    String s = "" 
    Int i = ArrayIndex 
    While i > 0 
        i -= 1 
        s += sArray[i] + "\n" 
    EndWhile 
    
    i = sArray.Length 
    While i > ArrayIndex 
        i -= 1 
        s += sArray[i] + "\n" 
    EndWhile 
    
    Debug.MessageBox(s) 
EndFunction

Of course it depends on what you're using the array for if this option would work or not.

Link to comment
Share on other sites

I'm thinking of using an array so that the contents will roll back if an earlier save is reverted to. I tried using PushString but I didn't know what the source file meant when it said:

; ## NOTE: The array has to be recreated each time you call this.

not sure how to 'recreate' an existing array:)

Since the object is to show a list in the MCM menu when toggled, it seems that PushString and SliceStringArray might reduce the amount of script activity outside of the MCM menu, I could check for the length of the array and SliceStringArray the array before displaying. But it's not clear how to use PushString.

 

diziet

Link to comment
Share on other sites

Here is an example of how to use those functions:

 

String[] sArray = new String[3] 
sArray[0] = "A"
sArray[1] = "B"
sArray[2] = "C" 

sArray = PapyrusUtil.PushString(sArray, "D") 
sArray = PapyrusUtil.SliceStringArray(sArray, 1)
Debug.MessageBox(sArray)

The result is sArray = ["B", "C", "D"]

Link to comment
Share on other sites

I think it just means that internally, that function has to recreate the array, so it's not super performance friendly. It should be fine to use occasionally, but don't spam the function and don't use it to populate an array.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...