Search Unity

Basic Question: JS 2D-Arrays in Unity?

Discussion in 'Scripting' started by PapaDragonov, Oct 13, 2015.

  1. PapaDragonov

    PapaDragonov

    Joined:
    Oct 13, 2015
    Posts:
    6
    Hey guys,

    I have a basic question about the Unity JavaScript functionallity. Maybe it is a noob question, maybe not. But I am one for sure. I do have some fundamental scripting experience, but it is really really outdated (2000-2002). So please forgive me, if I do not ask my question properly, or if it is a dumb question at all.

    I am starting a little indie gaming project with some friends and right now we are trying to find out which software environment to use. We would really like to (learn and) use Unity but our first steps brought up a serious question. Of course we have searched the web (and espacially the unity documentation and community) for answers. But since we do lack the necessary scripting skills by now, we were not able to find (respectively understand) an answer.

    So here is our question (hoping for a simple answer):
    Is ist possible to create JavaScript "Arrays of Arrays" in Unity? As far as we understand our scripting needs by now, we will certainly need this feature for our project and it would be a criterion for exclusion if Unity did not provide it. To explain a bit further: We will have lists of integers and strings. These lists should be listed themselves, so that we will be able to access certain values with X/Y "coordinates" within our "chart." As far as we know, JavaScript itself can handle these 2D-Arrays (?) without any problems. We already tested it with our old Adobe Director this way:

    array1 = [A, 1, B, 2]
    array2 = [B, 2, C, 1]
    ...

    finalArray = new Array(Array1, Array2, ...)

    In that case we were able to access our Values by finalArray[y][x]

    By now (again: without having real scripting skills) we were not able to achieve something similar with Unity. Are we just to blind to see the solution? Or is it really impossible? What finallycaused total confusion was the following: Somehwere in the Unity documentation we found the explicit statement, that Unity does not support JS Arrays of Arrays. We could have stopped here, but at the same time there are numerous forum posts out there, explaining ways to create 2D-arrays with Unity including code examples (which we unfortunately were unable to understand). So is there anybody out there who can help us out with a simple "yes, it is definitely possible like this..." or a "forget about Unity in that case..." ?

    Thanks a lot!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (javascript):
    1. // Actual 2D array
    2. var my2DArray = new int[20, 30]; // 20 X 30 int array
    3. my2DArray[5, 10] = 42;
    4.  
    5. // List of lists; note the different lengths
    6. var my2DList = new List.< List.<int> >();
    7. my2DList.Add (new List.<int>([1, 2, 3, 4, 5, 6]));
    8. my2DList.Add (new List.<int>([7, 8, 9, 10]));
    9. Debug.Log (my2DList[1][3]);
    --Eric
     
    PapaDragonov likes this.
  3. PapaDragonov

    PapaDragonov

    Joined:
    Oct 13, 2015
    Posts:
    6
    Hey Eric,

    thank you for the quick reply (wich we are totally able to understand. ;-))!

    But may I ask a further question? What about arrays (or lists) of various data types? (strings, integers, ...and am I right, that -0.25 for example is handled as an integer? ;-))
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401