Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Array syntax in unity for Javascript?

Discussion in 'Editor & General Support' started by taumel, Jun 11, 2006.

  1. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Hi,

    is there anywhere a complete documentation/working examples on how to use arrays in unity when using javascript with all the different types (float, Vector3,...)?

    How do i create defined or dynamic arrays with the different types exactly?
    How do i add and remove elements there, also without index?
    How do i access the elements?


    Regards,

    taumel
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Why not just use the .Net classes? They are available. It does not look like Unity supports the "normal" JavaScript array.
     
  3. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Note. This will be obsolete afte OTEE releases Unity v. 1.5, which according to the beta "adds an Array class which behaves exactly like ECMA 1.5 standard javascript."

    The current Javascript Array class is based on the .Net class library's ArrayList class. (Actually Boo.Lang.List) It is not typed, so you can't declare the type of the contents:

    Code (csharp):
    1.  
    2. var a=Array(); // creates an empty array
    3. var b=[1 , 2, 3, 4]; // creates an array containing 4 integers
    4.  
    To add elements you use the Add method:
    Code (csharp):
    1.  
    2. a.Add(someObject);
    3.  
    ... or using Insert

    Code (csharp):
    1.  
    2. a.Insert(4, someObject); // inserts element at place 4
    3.  
    Elements are accessed using normal array indexing:
    Code (csharp):
    1.  
    2. element4=a[4];
    3.  
    And the length is accessible through the Count property:

    Code (csharp):
    1.  
    2. size=a.Count;
    3.  
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Plus to remove a single element:
    Code (csharp):
    1.  
    2. a.RemoveAt(index);
    3.  
    Or to remove a range of elements:
    Code (csharp):
    1.  
    2. a.RemoveRange(startIndex, endIndex);
    3.  
    -Jeremy
     
  5. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Thanks,

    and how does this work if i use another type like Vector3 or floats for instance.? Is there any difference in the syntax then?


    Regards,

    taumel
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Arrays can only use integer indexes. Use Hashtable if you want to create an associatvie array.
     
  7. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    For clarity, you can store any data type you like in the array, so this is valid:

    Code (csharp):
    1. array[5] = myVector;
    ...but you can't use, for example, a vector as an index:

    Code (csharp):
    1. array[myVector] = 5;     // Not valid
    So, going back to taumel's question, you can use Vector3s, floats or anything else you like as data in an array, in the same way as you'd use integers.
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    [off-topic]
    To use Arrays in this way is bad practice in standard JavaScript anyway.
    [/off-topic]
     
  9. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Arrays are still not working for me - see the bug-report...


    Regards,

    taumel