Growing a Papyrus array

Post » Sun Jun 23, 2013 4:23 pm

I am encountering some weirdness with arrays. My ultimate goal is to add an item onto the end of an existing array. I have approached this in several ways and hit several obstacles.

Here's one way I thought of:

int[] Function ExtendArray(int[] array)    int counter = 0    int[] newArray = new int[array.length + 1]    while (counter < array.length)        newArray[counter] = array[counter]    endWhile    return newArrayEndFunction

This gives me the following error:

mismatched input 'array' expecting INTEGER

at the 3rd line. In other words, it doesn't like the fact that I am calling array.length inside the new array's length specification.

WHY!? The Creation Kit tutorial on arrays (which I don't have permission to link to) even shows creating a new array with its length defined as

i * x

. I'm doing the same thing!

On a side note, why don't my code blocks have any syntax highlighting, like the how to ask scripting questions stickied post (which I don't have permission to link to)?

Edit: Now I see that the syntax highlighting doesn't occur in the post preview, while it does once the post is actually posted.

User avatar
Jessica White
 
Posts: 3419
Joined: Sun Aug 20, 2006 5:03 am

Post » Sun Jun 23, 2013 4:16 pm

The wiki page states that you can't use variables/expressions in declaring arrays. http://www.creationkit.com/Arrays_(Papyrus)#Declaring_Arrays

For example:

int iLength = 5int[] iNewArray = new int[iLength]

does not work, but

int iLength = 5int[] iNewArrayif iLength == 5    iNewArray = new int[5]endIf

should work.


I didn't do the tutorial, but that directly contradicts the page above.

User avatar
Dalton Greynolds
 
Posts: 3476
Joined: Thu Oct 18, 2007 5:12 pm

Post » Sun Jun 23, 2013 7:20 pm

Thanks. That explains it. It is valid to use variables when getting array values, but not when specifying the length of a new array.

So, essentially what I have to do is this:

int[] Function ExtendArray(int[] array)    int counter = 0    int[] newArray    if (array.length == 1)        newArray = new int[2]    elseif (array.length == 2)        newArray = new int[3]    elseif (array.length == 3)        newArray = new int[4]    elseif (array.length == 4)        newArray = new int[5]    endif    while (counter < array.length)        newArray[counter] = array[counter]    endWhile    return newArrayEndFunction

Wow. That's INCREDIBLY stupid! Is there a better way to achieve this in Papyrus, or is Papyrus just stupid?

User avatar
Sheeva
 
Posts: 3353
Joined: Sat Nov 11, 2006 2:46 am

Post » Sun Jun 23, 2013 7:29 pm

The i * x thing is from exactly the same page you linked to. Just scroll down to the Getting/Setting Elements section. The difference is that they are specifying the index of an existing item, they are not specifying the length of a new array. I just assumed if you could do one then you could do the other, like any sane language.

User avatar
Danger Mouse
 
Posts: 3393
Joined: Sat Oct 07, 2006 9:55 am

Post » Sun Jun 23, 2013 5:05 am

Yeah it is kind of dumb and looks tedious. Another approach could be to use states. Not sure which is more efficient, going through a bunch of if statements or this method.

(untested)

int[] Function ExtendArray(int aiLength) ;aiLength is the length of the original array    string sLength = aiLength as string    GoToState("ArrayState"+aiLength)    int[] iNewArray = GenerateArray() ;generate array    ;do stuff to refill old valuesendFunctionint[] Function GenerateArray() ;needs to exist in empty stateendFunctionState ArrayState1    int[] Function GenerateArray()        GoToState("")        return new int[2] ;add one    endFunctionendState;state array2,3,4,5,etc
User avatar
ChloƩ
 
Posts: 3351
Joined: Sun Apr 08, 2007 8:15 am

Post » Sun Jun 23, 2013 2:44 pm

I'd rather just do something like this:
int[] aint ifunction OnInit() a = new int[N] i = 0endFunctionfunction Append(int v)  if (i < N)    a[i] = v    i += 1  endIfendFunction
N is a constant, replace with whatever your worst-case would be. N has to be <= 128 in case you didn't know.
User avatar
KRistina Karlsson
 
Posts: 3383
Joined: Tue Jun 20, 2006 9:22 pm


Return to V - Skyrim