Unity Community


Results 1 to 13 of 13

  1. Location
    Chicago
    Posts
    23

    Javascript Version?

    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. Location
    Unity Technologies HQ, Copenhagen
    Posts
    3,514
    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.
    Joachim Ante


  3. Location
    Chicago
    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. Location
    Guatemala
    Posts
    659
    We will be using C# mostly.
    Jeff McArthur | Guategeek
    http://www.bluegillstudios.com


  5. Location
    Oregon, USA
    Posts
    561
    Im using Javascript mostly.


  6. Location
    Vancouver, WA
    Posts
    295
    Mostly Javascript.


  7. Location
    Orlando, FL
    Posts
    750
    I am using all, but mainly Boo. JS is my second most common.


  8. Location
    Ontario, Canada
    Posts
    1,661
    - switch statement not supported
    -> You can use if instead, but we will add support for switch statements later on
    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.

    I would have thought switch statments would be standard?

    I am doing a Finite State Machine and the following code:
    Code:  
    1. switch (currentState)
    2. {
    3. case stateIdle: Idle();
    4.      break;
    5. case stateAttack: Attack();
    6.      break;
    7. case stateAfraid: Afraid();
    8.      break;
    9. case stateEvade: Evade();
    10.      break;
    11. case StateSleep: Sleep();
    12. }

    Is a lot more readable and especialy easier/quicker to write than:

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

    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
    GUI-eXtended 1.x is now Open-Source!
    https://bitbucket.org/JeremyHollings...ded_opensource


  9. Location
    Italy
    Posts
    468
    Something like this:

    Code:  
    1. switch(value){
    2. case 1:
    3.  dothis();
    4.  break();
    5. case 2:
    6.  dothat();
    7.  break();
    8. case 3:();
    9.  doother();
    10.  break();
    11. case 5: case 6case 7case 8:
    12.  docommon();
    13.  break;
    14. default:
    15.  donothing();
    16.  break;
    17. }

    which doesnt give me error on a browser with common javascript, but gives me a token error in unity !

    Oh yes please!


  10. Location
    Bloomington, IN, USA
    Posts
    367
    Huh? This works just fine for me:
    Code:  
    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.
    ~Francis
    Software Developer, WisdomTools, Inc.


  11. Location
    Germany
    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=00...70:iugs1ezlsfq ).
    So I think it might be beneficial for others, too, if this was updated...
    (*hides lazyness behind wise-cracking* :P ^^' )

    Thanks & Greetz, Ky.


  12. Location
    Zürich, Switzerland
    Posts
    26,572
    yes still up to date.
    its not related to the browser JS


  13. Location
    Germany
    Posts
    30
    Thanks for the confirmation =)

    Greetz, Ky.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •