Search Unity

using arrays

Discussion in 'Scripting' started by nmceri, Sep 19, 2005.

  1. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    I've never used an array before so I'm trying to arrange a simple test for myself to play around with, but I seem to be running into an error right away that I can't quite figure out.
    Code (csharp):
    1. var test = new Array(2);
    2. test[0]  = 0;
    3. test[1] = 0;
    I get "Array index is out of range". I'm sure this is quite simple but arrays are so foreign to me right now that I'm not sure how to correct this.

    Anyone know what I'm doing wrong?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    var test = new Array();
    test.Add(1);
    test.Add(2);

    The second array can not change its size.
     
  3. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    exactly what i needed! thank you.
     
  4. BasketQase

    BasketQase

    Joined:
    Sep 12, 2005
    Posts:
    97
    I'm having similar problems with arrays.

    I'm trying to make an array of floats and I tried the usual javascript syntax:

    Code (csharp):
    1.  
    2. var scores = Array(5);
    3.  
    4. scores[0] = num1;
    5. scores[1] = num2;
    6.  
    but I get the same compile error as the original post: "Array index is out of range." on the "scores[0] . . ." line.

    Then I saw this post, so I tried the following:

    Code (csharp):
    1.  
    2. var scores = new float[5];
    3.  
    and got this compile error: "Expecting LPAREN, found '['", followed by an unexepected token error.

    so then I tried

    Code (csharp):
    1.  
    2. var scores = new float(5);
    3.  
    And this gave me "The type 'System.Single' does not have a visible constructor that matches the argument list '(System.Int32)'." Not surprising, I guess.

    I then tried the other format given here:

    Code (csharp):
    1.  
    2. var scores = Array();
    3. scores.Add(1);
    4.  
    but the Add call causes an index out of range error as well. So failing all these I attempted to set the array at the same time as declaring it with this:

    Code (csharp):
    1.  
    2. var scores = Array( num1, num2, num3, num4, num5);
    3.  
    All the num variables are floats and the compiler just told me that there was no version of array that matched (System.Single, System.Single, System.Single, System.Single, System.Single)

    At this point I've run out of ways to declare an array. If anyone's got something I missed, I'd be very thankful to hear it, especially if it works ; )

    Thanks,
    -rob
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. var test = Array ();
    3. test.Add (1);
    4. test.Add (2);
    5. print (test[0]);
    6. print (test[1]);
    7.  
    I checked and the above script works in Unity 1.1.
     
  6. BasketQase

    BasketQase

    Joined:
    Sep 12, 2005
    Posts:
    97
    Indeed . . .

    I tried again and it does.

    Since I didn't know when I'd get a response, I went ahead and just wrote out my code explicitly (i.e. no array, and I just did my work code five times instead of using a for loop). Anyway, I say this because after I rewrote the code without any reference to arrays, I still had an "Array index out of range" on the same line that my "scores[0] . . ." line used to be. I then realized that the log had not cleared the old errors when it had re-imported the changed script.

    I imagine that the same thing happened when I'd attempted the dynamic style array. This makes more sense to me, since I couldn't figure out how scores.Add(1) would causing an out of range error.

    Thanks for the help. Sorry about the confusion, I assumed the error log cleared everytime a file was recompiled . . .

    -rob
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Compile errors are cleared, but what you got was runtime error.
    Runtime errors are cleared when you hit play again.
     
  8. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    I can't seem to use the length property for an array. I've tried all the variations I could think of, but this script won't compile.
    Code (csharp):
    1.  
    2. var test = Array ();
    3. test.Add (1);
    4. test.Add (2);
    5.  
    6. print (test.length);
    7. print (test[0]);
    8. print (test[1]);
    9.  
    Am I using it incorrectly?
    If I can't use length, is there an alternative?
     
  9. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Arrays in Unity javascript are based upon .Net's System.Collection.ArrayList and have a bit different interface from the Arrays in normal Javascript.

    The size is stored in the Count property:
    Code (csharp):
    1.  
    2. print(test.Count);
    3.  
     
  10. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Oh... and by the way....
    Code (csharp):
    1.  
    2. var a = new Array(10);
    3.  
    does neither create an array that contains a single elment with the value 10 nor an array with 10 empty elements. It creates an empty Array with intitial room for 10 elements. That is you can add up to 10 elements before the array code needs to allocate more storage space (which happens automatically). The parameter is just a hint to Mono how large the Array is going to be.

    If you want to create an array with an initial set of elements you can do it using the [ ] array syntax:
    Code (csharp):
    1.  
    2. var a = ["element0", "element1", "element2" , 42, 99, someObject];
    3.  
    Also unlike in regular Javascript, array indexes can only be numbers. If you want to create a dictionary (ie. an array where the keys are strings instead of integers) use Hashes instead:
    Code (csharp):
    1.  
    2. var h = new Hash();
    3. h["William Shakespeare"]="Bard";
    4. h["John Cleese"]="Ministry of Silly Walks";
    5. h["John Williams"]="Composer";
    6. h["Parrot"]="Dead";
    7.  
    Or with a inline syntax simliar to the [ ] array syntax:

    Code (csharp):
    1.  
    2. var h = {"William Shakespeare": "Bard",  "John Cleese": "Ministry of Silly Walks", "John Williams": "Composer", "Parrot": "Just resting"};
    3.  
     
  11. nmceri

    nmceri

    Joined:
    Aug 4, 2005
    Posts:
    56
    Thank you for the very comprehensive information! I had no idea i should be looking for .net array information. knowing how to search for the solutions goes a long way :)

    I also had a nice laugh with your extended examples... this parrot is no more!
     
  12. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    ... I'm forever emotionally scarred by all those Monty Python episodes.
     
  13. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    how would one make an array of gameobjects?
     
  14. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    For a fixed array size:
    Code (csharp):
    1.  
    2. var gos = [ new GameObject (), new GameObject (), new GameObject () ];
    3.  
    Or a variable array size:
    Code (csharp):
    1.  
    2. var test = Array ();
    3. test.Add (new GameObject ());
    4. test.Add (new GameObject ());
    5.  
     
  15. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    When I try create a hash i get:

    BCE0023: No apropriate version of 'Boo.Lang.Hash' for the argument list '()' was found.

    What am i doing wrong ?


     
  16. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    My bad... The class is called Hashtable and not Hash.
    Code (csharp):
    1.  
    2. var h = new Hashtable();
    3. h["William Shakespeare"]="Bard";
    4. h["John Cleese"]="Ministry of Silly Walks";
    5. h["John Williams"]="Composer";
    6. h["Parrot"]="Dead";
    7.  
    8.  
     
  17. socksy

    socksy

    Joined:
    May 21, 2005
    Posts:
    244
    [OT]Monty Python fan? :D