Search Unity

C# Multi-dimensional Arrays in Flash

Discussion in 'Flash' started by fanjules, Jan 3, 2012.

  1. fanjules

    fanjules

    Joined:
    Nov 9, 2011
    Posts:
    167
    I immediately noticed that none of my scripts with multi-dimensional arrays don't work when compiled to Flash. :(

    Somebody suggested using [][] notation instead of [,] but I wasn't entirely sure on how to. I'm quite new to C# but I did recognise [][] arrays as something that Actionscript uses. In Actionscript multidimensional arrays can be of irregular sizes along the length of the array, and that's exactly how the [][] style arrays work in C#. The downside is that the performance is crap... according to this page 8x slower than normal C# rectangular arrays: http://msdn.microsoft.com/en-us/magazine/cc163995.aspx

    Rectangular arrays are touted by Unity as being superfast (they refer to them as "builtin arrays" in Javascript). You will know how they work as follows:

    Declaration:
    float[,] myarray=new float[32,32];

    Find the lengths of each dimension:
    Lx=myarray.GetLength(0);Ly=myarray.GetLength(1);

    Get a value:
    n=myarray[5,3];

    Jagged arrays work as follows, heres the equiverlent declaration, noting that for each element on one axis you have to declare a whole new array within an array. Actionscripters will be familiar with this particularly laboursome task!
    float[][] myarray=new float[32][];
    for(n=0;n<myarray.Length;n++){myarray[n]=new float[32];}

    Find the lengths of each dimension (noting that each dimension could have a different length to it's neighbour since they are, after all - jagged!).
    Lx=mylength.Length;Ly=mylength[0].Length;

    Get a value:
    n=myarray[5][4];


    So, that's the work around but nobody wants to recode their entire project to use jagged arrays if they've been using nice and fast multidimensional arrays since day one (especially as declaring jagged arrays are considerably extra work too). Hopefully the Unity guys will convert our multi-dimensional rectangular arrays for us during compilation. There is no equiverlent in Actionscript, so essentially they will be turning our rectangular arrays into Actionscript jagged ones - but I see no reason why this is a problem - and we won't have to be forced to use jagged arrays, or write compiler directives to support both.