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

Javascript Version?

Discussion in 'Scripting' started by Scott-M, Jan 2, 2006.

  1. Scott-M

    Scott-M

    Joined:
    Jul 13, 2005
    Posts:
    23
    Hey guys,

    I looked through the forums, but didn't see an answer (search still not working?). What version of Javascript/ECMAScript are you guys using? I started prototyping some code in Firefox but realize Unity might not support all the latest JS 1.5 from Mozilla. Are you using a specific JS engine?

    Thanks and Happy New Year,
    Scott
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We are using a .NET based javascript implementation which was implemented by Rodrigo B. de Oliveira, the guy behind boo. http://boo.codehaus.org/

    It is loosely based on javascript 2.0.
    http://www.mozilla.org/js/language/js20/


    Most notable differences are:

    Our implementation is less dynamically typed. We are using a concept called type inference instead of dynamic typing for performance reasons.
    Type inference means that the compiler tries to determine the type by the value you assign to the variable. If it can not be determined the variable becomes dynamically typed.

    The drawback of dynamically typed languages is that they execute much slower. (Because the type of variables is not known in advance)
    The main advantage of dynamically typed languages is that you have to write less code and don't need to express your intention twice eg.
    string b = "Hello"; // here it is already implied that b is a string because of the assigment, so why write it down twice?

    With type inference you get the best of both worlds.

    People can write javascript code, which is often much more convenienent and easier but the code still executes as fast as statically typed languages like C#.

    eg. this is legal in mozilla js
    var p = "test";
    p = 1;

    but will yield a compile error in unity's javascript compiler.
    This is another advantage of type inference. Programming errors are visible much earlier, that is at compile time instead of run time.

    Other differences are:

    - No prototype based inheritance
    -> You can use normal class based inheritance

    - switch statement not supported
    -> You can use if instead, but we will add support for switch statements later on


    All in all, writing our own javascript implementation was a necessity. In an earlier version of unity we tried using python for game code, which was simply too slow, for how much game code we were executing every frame.

    With our own javascript implementation, we can get to half the speed of C++. Which is around 10-20 times more than what mozilla's js implementation can do.
     
    7a likes this.
  3. Scott-M

    Scott-M

    Joined:
    Jul 13, 2005
    Posts:
    23
    Thanks for the detailed reply -- much appreciated!

    Just curious, are you all using JavaScript for the game you've got in development?
     
  4. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    We will be using C# mostly.
     
  5. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Im using Javascript mostly.
     
  6. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Mostly Javascript.
     
  7. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I am using all, but mainly Boo. JS is my second most common.
     
  8. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Why on earth are switch statments not supported? I just tried to use one and spent the last 15mins trying to figure out why it was throwing an error. :eek:

    I would have thought switch statments would be standard?

    I am doing a Finite State Machine and the following code:
    Code (csharp):
    1.  
    2. switch (currentState)
    3. {
    4. case stateIdle: Idle();
    5.      break;
    6. case stateAttack: Attack();
    7.      break;
    8. case stateAfraid: Afraid();
    9.      break;
    10. case stateEvade: Evade();
    11.      break;
    12. case StateSleep: Sleep();
    13. }
    14.  
    Is a lot more readable and especialy easier/quicker to write than:

    Code (csharp):
    1.  
    2. if (currentState == stateIdle){
    3.   Idle();
    4. }
    5. else if (currentState == stateAttack){
    6.   Attack();
    7. }
    8. else if (currentState == stateAfraid){
    9.   Afraid();
    10. }
    11. else if (currentState == stateEvade){
    12.   Evade();
    13. }
    14. else if (currentState == stateSleep){
    15.   Sleep();
    16. }
    17.  
    18. /* and on and on for all of the states and state transitions*/
    19.  
    When do you guys plan on supporting switch?

    I think we need a collection of pages that tell what is supported in Unity with the various languages, max world size, max clipping planes, what the max polycounts for imported meshes are (I just learned on the forum that the tri limit for imported meshes is around 60,000 per mesh. I haven't found that info anywhere else), etc. I really think it would save a lot of headache. :)

    Anyway, thanks guys.
    -Jeremy
     
  9. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Something like this:

    Code (csharp):
    1.  
    2.  
    3. switch(value){
    4. case 1:
    5.  dothis();
    6.  break();
    7. case 2:
    8.  dothat();
    9.  break();
    10. case 3:();
    11.  doother();
    12.  break();
    13. case 5: case 6: case 7: case 8:
    14.  docommon();
    15.  break;
    16. default:
    17.  donothing();
    18.  break;
    19. }  
    20.  
    21.  
    which doesnt give me error on a browser with common javascript, but gives me a token error in unity !

    Oh yes please!
     
  10. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Huh? This works just fine for me:
    Code (csharp):
    1. enum State {
    2.     IDLE,
    3.     ATTACK,
    4.     AFRAID,
    5.     EVADE,
    6.     SLEEP,
    7. }
    8.  
    9.  
    10. var state : State;
    11. var stateString : String;
    12.  
    13. function Update () {
    14.     switch (state) {
    15.         case State.IDLE:
    16.             stateString = "Idle";
    17.             break;
    18.         case State.ATTACK:
    19.             stateString = "Attack";
    20.             break;
    21.         case State.AFRAID:
    22.             stateString = "Afraid";
    23.             break;
    24.         case State.EVADE:
    25.             stateString = "Evade";
    26.             break;
    27.         case State.SLEEP:
    28.             stateString = "Sleep";
    29.             break;
    30.         default:
    31.             break;
    32.     }
    33.     Debug.Log(stateString);
    34. }
    The code posted by Litobyte doesn't work because you can't put multiple cases without a statement between them, but switches seem to work just fine in Unityscript/Javascript if you use the right syntax.
     
  11. SisterKy

    SisterKy

    Joined:
    Dec 6, 2009
    Posts:
    30
    This Thread is 4 Years old now... I'd like to ask, how much of this is still up to date?
    Especially "It is loosely based on javascript 2.0."?

    I'll admit I haven't spent hours searching; but this is the first hit for "Javascript version" on the customized unity search ( http://www.google.com/cse/home?cx=002470491425767499270:iugs1ezlsfq ).
    So I think it might be beneficial for others, too, if this was updated... :roll:
    (*hides lazyness behind wise-cracking* :p ^^' )

    Thanks Greetz, Ky.
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    yes still up to date.
    its not related to the browser JS
     
  13. SisterKy

    SisterKy

    Joined:
    Dec 6, 2009
    Posts:
    30
    Thanks for the confirmation =)

    Greetz, Ky.