Search Unity

Declaring a Bi-directional array as a built in array, is it possible?

Discussion in 'Scripting' started by DwarfCavern, May 29, 2011.

  1. DwarfCavern

    DwarfCavern

    Joined:
    Mar 10, 2011
    Posts:
    31
    Hi all,

    I am having troubles converting my 2-dimensional array to a built-in type of array, mainly because I don't know how to declare it :confused:.
    In case this wasn't clear, here is an example:

    if I want to declare a built-in array of integers or vector3's I do:

    Code (csharp):
    1. var intArr : int[] : new int[5];
    2. var vector3Arr : Vector3[] : new Vector3[5];
    and that is fine, but what do I do when I have an array like this:

    Code (csharp):
    1. var multiArr = new Array();
    2. multiarr = ([Vector2, Vector2, Vector2])
    I tried using [][] but unity didn't recognize it. Please help :) Thanks! ;)
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You wouldn't want to use Array. Use multidimensional built-in arrays, such as

    Code (csharp):
    1. var int2DArray = new int[5, 5];
    --Eric
     
  3. DwarfCavern

    DwarfCavern

    Joined:
    Mar 10, 2011
    Posts:
    31
    Hi Eric, thanks for a quick reply. But I am not sure how to use multidimensional built in arrays, can you pssibly give me an example that would use Vector2's? so that I can more clearly understand, since [5,5] can be confusing when I don't know which is which :)

    so how would I declare a multidimensional array that contains 4 vector 2's?

    Thanks!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm not sure what this means, since if you have 4 Vector2s, you only need a single-dimensional array, or possibly a 2D array which is 2 across and 2 down. How about this instead:

    Code (csharp):
    1. var v2Array = new Vector2[3, 4];
    That makes a 2D array of Vector2s, which is 3 across and 4 down, for a total of 12. So you can access them like

    Code (csharp):
    1. v2Array[0, 1] = Vector2.one;
    2. v2Array[2, 3] = Vector2(1.5, 1.5);
    --Eric
     
  5. GisleAune

    GisleAune

    Joined:
    May 16, 2011
    Posts:
    88
    edit: ninja'd.
     
  6. DwarfCavern

    DwarfCavern

    Joined:
    Mar 10, 2011
    Posts:
    31
    ah great, thanks :) that's what I was looking for :) *cheers*
     
  7. DwarfCavern

    DwarfCavern

    Joined:
    Mar 10, 2011
    Posts:
    31
    Hi again,

    I still am having problems with this issue. I guess I wasn't clear enough originally so I will try to explain better now what exactly I need.

    I need to be able to create and populate a twodimensional array whose each element is an array of Vector 2's, so that I can declare it's elements like this:

    Code (csharp):
    1. v2array[0,1] = ([Vector2.one, Vector2.zero, Vector2(1.5,1.5]);
    I am sorry that I wasn't clear enough in my original post, I am fairly new to programming so I am trying not to be lost in multidimensional arrays :D

    I have tried declaring the array as:
    Code (csharp):
    1. var 2dArray : Vector2[,];
    but logically that doesn't work because what that expects is to have a single Vector2 in each of the elements, not an array of Vector2's... I hope this is a bit clearer?

    Thanks in advance!
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A 2D Vector2 array is like a grid, or spreadsheet, where each cell contains a Vector2. If each row contains the same number of Vector2s, then that's what you'd use. If each row contains a different number of Vector2s, then you can use a jagged array instead:

    Code (csharp):
    1. var 2dArray = [ [Vector2(1,1), Vector2(2,2)], [Vector2(3,3), Vector2(4,4), Vector2(5,5)] ];
    2. print (2dArray[1][2]);
    Note that declaring the type directly in JS currently isn't implemented, so this results in an error:

    Code (csharp):
    1. var 2dArray : Vector2[][];
    For a workaround, see here.

    --Eric
     
  9. DwarfCavern

    DwarfCavern

    Joined:
    Mar 10, 2011
    Posts:
    31
    Thanks! Now I got my mind about it and I think I know how to use a 2D array for what I need :) Thanks again!