Search Unity

Array Weirdness?...

Discussion in 'Scripting' started by littlelingo, Nov 8, 2006.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Hi All,

    I have created a Vector 2 and have a multdimensional array. I am then grabbing the x and y values of the Vector2 to perform a lookup in the Array. The problem is, it only half works and/or gives me an error. Can anyone see if I am doing something wrong or not doing something correct or is this some sort of issue?

    Here is a rough example of what I am doing:

    Code (csharp):
    1.  
    2.  
    3. var grid = new Array();
    4. grid.Push(new Array(1,1,1,1));
    5. grid.Push(new Array(1,1,1,1));
    6. grid.Push(new Array(1,1,1,1));
    7.  
    8. var tester = Vector2(2,2);
    9. print (gridMap[tester.x][tester.y]);
    10.  
    11.  
    There error I am getting is:

    ArgumentException: parameters
    Boo.Lang.RuntimeServices.GetSlice(System.Object target, System.String name, System.Object[] args) [0x00000]

    The half works part is I can use either .x or .y to get the front part of the array but using both in combination gives me the error. In fact, I can even use the .x or .y and then use a number as the second accessor and it will work.

    Thanks in advance for the help.

    Regards,

    -- Clint
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Vector2.x is of type float and not int. Floats can't be used to index arrays.
    Instead you can use use an int.
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I understand that it shouldn't work but it does for the first portion of it. For example:

    Code (csharp):
    1.  
    2.  
    3. var tester = Vector2(2,2);
    4. print (gridMap[tester.x]);
    5.  
    6.  
    That will return the proper item. So maybe this is something that should be checked when compiled?

    Also, how do I do an inline float to integer conversion? In normal Javascript I could use parseInt, parseFloat and some others but those give me an error... I have tried Number, Integer, etc. and those all throw errors as well...

    So as an example this is what I am doing when trying that conversion:

    Code (csharp):
    1.  
    2.  
    3. var tester = Vector2(2,2);
    4. print (gridMap[Integer(tester.x)]);
    5.  
    6. or
    7.  
    8. print (gridMap[Number(tester.x)]);
    9.  
    10. or
    11.  
    12. print (gridMap[parseInt(tester.x)]);
    13.  
    14.  
    15.  
    Thanks!

    -- Clint
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can do conversion to int like this. We are going to add parseInt and parseFloat with the next release.

    Code (csharp):
    1.  
    2. var grid = new Array();
    3. grid.Push(new Array(1,1,1,1));
    4. grid.Push(new Array(1,1,1,1));
    5. grid.Push(new Array(1,1,1,1));
    6.  
    7. var tester = Vector2(2,2);
    8. function Start ()
    9. {
    10.     var testerXInt : int = tester.x;
    11.     var testerYInt : int = tester.y;
    12.     print (grid[testerXInt][testerYInt]);
    13. }
    14.  
     
  5. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    DOH! Okay, thanks. ;-)

    Appreciate the help once again Joe!

    -- Clint