Weird array index behavior

Post » Sat Mar 01, 2014 1:49 pm

Over the past few months I've noticed weird things happen when you try to use certain expressions or functions inside of array index brackets.

For instance, I realized that you cannot use MyArray.Find(var) inside of MyArray's index brackets (and added a note to the wiki). This will compile fine but it will result in really weird things happening at runtime:

MyArray[MyArray.find(targetValue)] = newValue

Today I also noticed something far more bizarre. If you do something like this (I did it in a loop, but here's what it would look like written out):

Spoiler
i = 0MyArray[i * 5] = 32MyArray[i * 5 + 1] = 33MyArray[i * 5 + 2] = 34MyArray[i * 5 + 3] = 35MyArray[i * 5 + 4] = 36i = 1MyArray[i * 5] = 37MyArray[i * 5 + 1] = 38MyArray[i * 5 + 2] = 39MyArray[i * 5 + 3] = 40MyArray[i * 5 + 4] = 41i = 2MyArray[i * 5] = 42MyArray[i * 5 + 1] = 43MyArray[i * 5 + 2] = 44MyArray[i * 5 + 3] = 45MyArray[i * 5 + 4] = 46

You will end up with an array that looks like this:

Spoiler
MyArray[0] = 32MyArray[1] = 0MyArray[2] = 0MyArray[3] = 0MyArray[4] = 0MyArray[5] = 37MyArray[6] = 5MyArray[7] = 5MyArray[8] = 5MyArray[9] = 5MyArray[10] = 42MyArray[11] = 10MyArray[12] = 10MyArray[13] = 10MyArray[14] = 10

In other words, it seems that papyrus evaluates the index correctly, and then for some reason ignores the expression after the equals sign and instead assigns the first part of the expression inside the index brackets to the array value. Weeeeiiiirrrd :tongue:

I tested it a bit more and it seems to work whenever there is only one operator sign inside the brackets (like MyArray[i + 1] or MyArray[i * 4]) but if you add more than that, things start getting a bit trippy. I think I've gotten to the point now that I'm just going to be afraid to stick anything besides a single number or variable inside array index brackets. :D

Food for thought...

User avatar
Laura-Lee Gerwing
 
Posts: 3363
Joined: Fri Jan 12, 2007 12:46 am

Post » Sat Mar 01, 2014 11:50 am

Heh, really weird.

As a matter of interest, did you try sticking parentheses into your expressions to ensure correct order of evaluation? For example

MyArray[(i * 5) + 1] = 33

Or even

MyArray[((i * 5) + 1)] = 33

And if so, did it still misbehave?

Plus, are you absolutely sure there weren't any errors in your loop? Does it still misbehave when you use unlooped statements (as in your example)?

User avatar
The Time Car
 
Posts: 3435
Joined: Sat Oct 27, 2007 7:13 pm


Return to V - Skyrim