Search Unity

How to use generics in unity javascript

Discussion in 'Scripting' started by Ippokratis, Feb 27, 2011.

  1. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    This is some code based on http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html and http://msdn.microsoft.com/en-us/library/y52x03h2.aspx
    It showcases the use of generics in unity js.

    Code (csharp):
    1.  
    2. import System.Collections.Generic;
    3.  
    4. function Start ()
    5. {
    6.     var list : List.<String>;
    7.     list = new List.<String>();
    8.     //Capacity is the number of elements that the List<T> can store before resizing is required
    9.     //Count is the number of elements that are actually in the List<T>.
    10.     Debug.Log("list capacity is " +list.Capacity);
    11.     Debug.Log("list count is " +list.Count);
    12.     Debug.Log("Adding 4 entries");
    13.    
    14.     list.Add("Generics");
    15.     list.Add(" work");
    16.     list.Add(" in");
    17.     list.Add(" js");
    18.     list.Add(" too.");
    19.    
    20.     Debug.Log("Note that now capacity is bigger than count");
    21.  
    22.     Debug.Log("list capacity is " +list.Capacity);
    23.     Debug.Log("list count is " +list.Count);
    24.    
    25.     //js cannot have foreach but can use for in
    26.     var sentence:String;
    27.  
    28.     for( var word:String in list)
    29.     {
    30.         sentence +=word;
    31.     }
    32.     Debug.Log(sentence);
    33.     Debug.Log("list.Contains(\"generics\") is " + list.Contains("generics"));
    34.     Debug.Log("list.Insert(5, \" Unity js rocks !\");" +" inserts a new item at the end of the list");
    35.     list.Insert(5, " Unity js rocks !");
    36.     Debug.Log("Let's check it");
    37.     sentence = "";
    38.    
    39.     for( var word:String in list)
    40.     {
    41.        sentence +=word;
    42.     }
    43.     //For more methods of List check out http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
    44.     //Or google c sharp List
    45.    
    46.  
    47. }
     
    Last edited: Feb 3, 2012
    DrKucho and rosaSchleim like this.
  2. obliviux

    obliviux

    Joined:
    Mar 17, 2011
    Posts:
    137
    Thanks for this it is very helpful
     
  3. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Code (csharp):
    1.  
    2.     list.Add("Generics");
    3.     list.Add(" created");
    4.     list.Add(" in");
    5.     list.Add(" C#");
    6.     list.Add(" etc.");
    7.     list.Add(" can");
    8.     list.Add(" be");
    9.     list.Add(" accessed");
    10.     list.Add(" by");
    11.     list.Add(" js");
    12.     list.Add(" (us)");
    13.     list.Add(" too.");
    14.  
    FTFY
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You didn't "fix" anything though.

    --Eric
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Not relevant with the topic specifically, but can anyone tell me why i should use java script rather than c# ?
    Does it make a difference on mobile platforms or something?
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    By and large it's personal preference.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Really, please don't start that up here, it's been discussed to death and back.

    --Eric
     
  8. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,

    obliviux : If you also feel like sharing some bits of code that demonstrate how to use .NET features in unityscript ( or js ), please do so, we all need as much help as possible.

    NPSF3000 :

    Eric5h5: FTFY has a double meaning... ( Those evil C# coders again )...

    Aubergine : Indeed, it is more about personal preference IMHO too.
     
    Last edited: Mar 31, 2012
  9. dsjunnesson

    dsjunnesson

    Joined:
    Dec 3, 2012
    Posts:
    15
  10. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,
    Looks really useful, thanks for sharing.