Search Unity

Associative arrays

Discussion in 'Scripting' started by d@mmi, May 3, 2008.

  1. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    In Unity, Is there a way I can set up either associative arrays

    Code (csharp):
    1. value{key} = float
    or

    just as well, can I explicity define multi-dimensional arrays like

    Code (csharp):
    1. value[xInt][yInt] = float
     
  2. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    If you're using Javascript, then you can't use multi-dimensional arrays. C# does support them though.

    For an associative array, use Mono's hashtable: http://msdn.microsoft.com/en-us/library/system.collections.hashtable(VS.71).aspx

    If you were using C#, you could use a Dictionary also.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var foo = new Hashtable(); // Or "var foo = {};" if you want to be really terse
    2. foo["something"] = "blah";
    You can use multi-dimensional arrays in Javascript.

    Code (csharp):
    1. var foo = new Array(10);
    2. foo[0] = new Array(10);
    3. foo[0][0] = "blah";
    You can even use multidimensional built-in arrays in Javascript:

    Code (csharp):
    1. var tdata = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 32, 32);
    2. tdata[10, 25] = .75;
    You just can't define them from scratch, apparently, for some reason.

    --Eric
     
  4. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    Thanks a lot for your advice.

    I'll give it a go.