Search Unity

Dynamic Multi-dimensional Arrays

Discussion in 'Scripting' started by SophieHoulden, Mar 21, 2009.

  1. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    So I'm pretty happy scripting in unity so far, most of my actionscript knowledge moved to C# without any major issues, but there is something thats bugging me, I like to keep most of my level data in dynamic multidimensional arrays, its probably lazy on my part but its what I'm used to, and was kind of hoping it could be done so my old code can be ported.

    anyway, heres where I'm up to with it:
    Code (csharp):
    1. ArrayList myList = new ArrayList();
    2. myList.Add("testyA");
    3. myList.Add(9001);
    4. myList.Add("testyC");
    and that all works fine, and I can even get away with:
    Code (csharp):
    1. myList.Add(new ArrayList());
    but when I try to add to the new ArrayList like so:
    Code (csharp):
    1. myList[myList.Count-1].Add("plzDontCrash");
    that throws:
    error CS0117: `object' does not contain a definition for `Add'

    so I try casting it:
    Code (csharp):
    1. (ArrayList)myList[myList.Count-1].Add("plzDontCrash");
    which throws:
    error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    any suggestions?
     
  2. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    240
    I found that casting like this works better:
    Code (csharp):
    1.  
    2. ((ArrayList)(myList[myList.Count-1])).Add("plzDontCrash");
    3.  
    P.S: Yes, I'm very paranoid about compilers misinterpreting my casts :D
     
  3. SophieHoulden

    SophieHoulden

    Joined:
    Mar 19, 2009
    Posts:
    20
    thanks myraneal! you arent paranoid as that seems the simplest way of doing it, its unwieldly code but then I guess so are my practices :p

    anyway, thanks again, I can dive back in now :D
     
  4. Omega

    Omega

    Joined:
    Jul 31, 2007
    Posts:
    125
    For what it's worth, UnityScript (aka "javascript") is closer to flash's actionscript (I think) and doesn't need casts. If you were using Unityscript, or even Boo for that matter, all of this could be avoided :wink: .

    I say this because casting is one of the few things in C# that really irks me. (Right after the "GameObject object = new GameObject();" nonsense)


    Edit: Also for what it's worth, a more object-oriented approach might work better, but since "level data" is a bit vague, I'm not sure. If it's reasonably static in structure, you could do something like (in Unityscript, I couldn't get it working right in C#):
    Code (csharp):
    1. var levels : LevelData[];
    2.  
    3. class LevelData
    4. {
    5.   var varname : String;
    6.   var difficulty : int;
    7. }
    This would give you a nice array in Unity's inspector, allowing the values to be changed without even touching the code. I've used this trick before, by the way. It's very powerful when used properly.

    Hope it helps.