error with array function

I’ve started getting an error on a piece of code that was fine for a long time and which I haven’t changed. I’m getting this:

“Type ‘Object’ does not support slicing”

It’s showing for every line where I try to read an item out of an array. The array is returned by a function and contains nested arrays. So my code is like this:

var returnedArray = createArray(); // function that returns the nested array
 
for (var col : int = 1;col < 9;col++) {
    var intFromArray : int = returnedArray[1][col]; // this errors
    var stringFromArray : String = returnedArray[2][col]; // this errors
}

I don’t understand why I’m now getting the errors and reviewing the information I can find about unity arrays hasn’t given me any ideas. Can anyone help?

Presumably you’re using #pragma strict now. Take that out and it should work again.

–Eric

Sorry I should have mentioned that this is an iphone project so #pragma strict is mandated. That said it has been compiling for iPhone OK, so I wouldn’t think that’s the problem…?

(Edit) my answer assumed you were using nested arrays, if you are using multidemensional arrays in JS (as seems likely after reading the reply below mine) I think my code will not work, I don’t know JS syntax enough to be sure.

My guess would be that createArray() has changed, but it is just a guess. To investigate I would change the code to the below(it may not be syntactily correct I don’t regularly program in JS)

var returnedArray = createArray(); // function that returns the nested array 
  
for (var col : int = 1;col < 9;col++) { 
    var intArray : array = returnedArray[1];
    var intFromArray : int = intArray[col]; 
    var stringArray : array = returnedArray[2]; 
    var stringFromArray : String = stringArray[col]; 
}

this will show you exactly what ‘Object’ “Type ‘Object’ does not support slicing” is refering to. When I get that error in Boo it means either I screwed up (common), or that the compilier is confused about what type is what,so it may also solve the problem. In Boo atleast if the compler is uncertain of the types it will believe you and if you are wrong you will get a run time error. So even if it compiles it my still be wrong and give you a rn time error.

Cheers,
Grant

To what you will find on the iphone section of this board, JS + multi dim arrays will not work.

you have 2 options:

  1. 1 large array and do the 2D indices maths yourself (i = x + width * y where i is the index in the 1D array, x and y are the coordinates in the 2D and width is the length of each line in X direction)

  2. Array of array … not really what you likely will want to use.

:smile: thanks for the help guys, I’ve scrapped the multidimensional array and just made it a flat one and after some experimentation it looks like I needed to declare the array type in a way I didn’t know you could:

var returnedArray : int[ ] = createArray();

worst part is this stuff seems so obvious after you’ve figured it out :?

Actually it DOES work - #pragma strict just requires you to be REALLY specific:

var intFromArray : int = (returnedArray[1] as Array)[col];

in my 3d array this gets really ugly:

result : int = ((arr[×] as Array)[y] as Array)[z];