javascript multi dim arrays

Upload: elie-haykal

Post on 08-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Javascript Multi Dim Arrays

    1/3

    ascriptMultiDimArrays - Unify Community Wiki

    4/13/2011 10:12//www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays

    avascriptMultiDimArrays

    rom Unify Community Wiki

    uthor: Eric Haines (Eric5h5)

    Contents1 Description2 Usage3 Jagged Arrays4 C# - MultiDim.cs

    escription

    OTE: This is largely obsolete in Unity 3.2 , since multidimensional arrays can be declared directly in JS now. Please upgrade if you haven't alread

    owever, the syntax for directly declaring the type of jagged multi-dimensional arrays is still missing in JS, so if you need those, this script can stil

    eful (though you can delete the rectangular array functions and just leave the jagged array functions).

    ulti-dimensional (AKA rectangular) arrays in Javascript are a bit of a conundrum, prior to Unity 3.2. Ideally it should be possible to write something likeo = new int[5,6];". Alas, this results in a compiler error. However, it is possible to write this:

    var heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 10, 10);print (heights);

    he output, assuming a terrain is present, is "System.Single[,]" (AKA "float[,]"). And you can use the array as expected with no issues. So, clearly these so

    rays are implemented in Javascript, with the exception of the ability to declare them. This makes them only marginally useful, unless you're working with

    rrain data.

    r are they? Javascript uses type inference, so it's not necessary to specify the type if the compiler can figure it out. For example, "var foo = 5;" results in "

    ing declared as an int, since an int value was specified. Likewise, GetHeights() returns a float[,], so "heights" is declared as a float[,] through type inferen

    e can use this feature, together with the fact that there's no problem declaring rectangular arrays in C#, to allow us to declare arrays like this in Javascripell with minimal fuss.

    Usage

    t the MultiDim script below in your Standard Assets/Scripts folder (if you don't have one, make one). This way it can be accessed from Javascript easily

    thout having to worry about compilation order problems. Now you can use the MultiDim class and type inference to declare rectangular 2D and 3D arra

    ts, floats, and strings. For example:

    var foo = MultiDim.IntArray(100, 200);

    foo[52, 49] = 123;

    var

    foo2 = MultiDim.IntArray(100, 200, 300);foo2[99, 199, 299] = 1;

    var numbers = MultiDim.FloatArray(50, 60);

    numbers[10, 20] = Mathf.PI;

    var someStuff = MultiDim.StringArray(10, 10, 10);

    someStuff[0, 0, 7] = "w00t";

    you need to use additional types or dimensions, it should be pretty obvious how to make new functions in the MultiDim script that will return these, eve

    u don't know C#...just use the existing functions as a template.

  • 8/7/2019 Javascript Multi Dim Arrays

    2/3

    ascriptMultiDimArrays - Unify Community Wiki

    4/13/2011 10:12//www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays

    agged Arrays

    ong the same lines, jagged arrays (where each row can have a different number of columns) can be made in Javascript by using an array of arrays. But if

    ould rather use jagged built-in arrays for speed instead of using dynamic arrays, the method for being able to declare them directly is missing, though the

    ll be used. The work-around uses the same technique as above, so you can use JaggedInt, JaggedFloat, and JaggedString to declare these sorts of arrays b

    ssing in a value representing the number of rows. Each row is null by default and must be declared individually. For example:

    var foo = MultiDim.JaggedInt(3);

    foo[0] =new

    int[5];foo[1] = new int[21];

    foo[2] = new int[7];

    foo[1][19] = 55;

    s also possible to declare jagged arrays using type inference with initial values:

    var foo = [ [1, 2, 9], [4, 5, 2], [0, 0, 7] ];

    he type of "foo" in this case is System.Int32[][] (AKA int[][]). If you use this method, you don't need the MultiDim script.

    # - MultiDim.cs

    public class MultiDim {

    public static int[,] IntArray (int a, int b) {

    return new int[a,b];

    }

    public static int[,,] IntArray (int a, int b, int c) {

    return new int[a,b,c];}

    public static float[,] FloatArray (int a, int b) {

    return new float[a,b];

    }

    public static float[,,] FloatArray (int a, int b, int c) {

    return new float[a,b,c];

    }

    public static string[,] StringArray (int a, int b) {

    return new string[a,b];}

    public static string[,,] StringArray (int a, int b, int c) {

    return new string[a,b,c];

    }

    public static int[][] JaggedInt (int a) {

    return new int[a][];

    }

  • 8/7/2019 Javascript Multi Dim Arrays

    3/3

    ascriptMultiDimArrays - Unify Community Wiki

    4/13/2011 10:12//www unifycommunity com/wiki/index php?title=JavascriptMultiDimArrays

    public static float[][] JaggedFloat (int a) {

    return new float[a][];

    }

    public static string[][] JaggedString (int a) {

    return new string[a][];

    }

    }

    etrieved from "http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays"

    ategories: Concepts | C Sharp

    This page was last modified 17:41, 27 March 2011.