Search Unity

c# or javascript

Discussion in 'Scripting' started by raoul, Jul 29, 2007.

  1. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Hi all,

    I am still in the process on looking how good unity is and meanwhile raising the funds from "somewhere?" to get me a mac and Unity!

    Having spend the last year on game development I now realise that one of the hardest things to tackle when making a good quality game is the performance issue.

    One of the reasons I "think" I like Unity is because it has the javascript language integrated which I am quite familiar with. I now wonder whether javascript is equally fast as c# in Unity when ofcourse properly used!?

    Any comments on this?

    Thanks,
    Raoul
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes, all languages supported by Unity are equally fast, they are all compiled to the same .NET bytecode. (That is Unity's JavaScript is around 10-20x faster than web javascript)

    However, in my experience what makes most games slow is not the choise of the language. Most often it's bad scene organization (using too much objects and so on) or using programming constructs that are just slow by definition (i.e. instead of telling "you're hit by a rocket" directly to some game object, the code sends "this object is being hit by a rocket" message to everyone). And so on.
     
  3. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    I'm using C#, however probably the only thing that I think that Javascript misses is the ability to synchronize access to variables. For example, if you have two threads (correction coroutines don't run on threads) running, and only one can access a list at once.

    But in this extreme case you can create a synchronized list in C#, put it in a folder that is compiled before, and then use Javascript.

    For beginners I think that Javascript is much easier.

    As for code speed, the problems are most likely to come from your code. For example, doing this:
    Code (csharp):
    1.  
    2. for( int i = 0; i != someList.Count; ++i )
    3.     // do something...
    4.  
    is slower than doing:
    Code (csharp):
    1.  
    2. for( int i = 0, limit = someList.Count; i != limit; ++i )
    3.     // do something...
    4.  
    In small lists the difference may be very small (or not noticeable at all), but in big lists the difference can be quite big.

    Regards,
    Afonso
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Coroutines do not use threads. Thus there is no need to synchronize using mutexes or other threading concepts.

    If it was using threads, that would really be a nightmare as everybody would run into the usual threading issues without evening knowing about it. On top of that having lots of coroutines running at the same time would be pretty slow if it was based on real threads due to the often short running time. Coroutines use a much lighter weight backend. Essentially it simply stores all local variables as member variables in an automatically generated class instead of just on the stack. Thus it can always be stored away and continued later.
     
  5. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Thanks for correcting me, I really thought that coroutines ran in parallel.

    Regards,
    Afonso
     
  6. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Thanks for that!