Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why does C# over complicate things compared to JavaScript?

Discussion in 'Scripting' started by Caelorum, Sep 26, 2014.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    For example this in Java script:
    Code (JavaScript):
    1. rigidbody2D.velocity.x = 26;
    Has to be this in C# script:
    Code (CSharp):
    1.  
    2.                 Vector2 xVal = rigidbody.velocity;
    3.                 xVal.x= 26f;
    4.                 rigidbody2D.velocity = xVal;
    Am I coding incorrectly? With bad habits? Or is C# just way too complicated sometimes.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Oh lord, what a flamebait post this is :)

    There's a reason. Vector2/3 is a struct which is treated as a value type. Velocity is a property. Properties backed by value types return a copy instead of the actual property. Therefore, you'd be manipulating a copy of the velocity and not the actual velocity. They've simply been nice enough to abstract that away for you in the JS implementation.

    One could easily make the argument for things that C# does better than Unity's JS implementation - but that could quickly spiral into a language war...which this thread might still do.
     
    Caelorum likes this.
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    In the end, I'm pretty sure the compiled CIL is exactly - or very near - the same.

    In US - since it's not really JavaScript - I have the feeling this kind of shortcut could lead to error of how value types are handle, without you being able to know it. Like Kelso said, value type are passed around by copy, so the first example in US should logically fail. If it does not, one has to make some assumption on how they are handled.
     
    Caelorum likes this.
  4. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    I'm not trying to start a flamebait or anything. Don't want people fighting over which language is better. I'm trying my hardest to learn in c# because I have c++ background. But all of this added stuff like Vectors/Rigidbodies from Unity's API is just painful. So many errors left and right, so much added code.

    And all of these little things like "Manipulating a copy and not the actual copy". Where do i learn that from? All I'm seeing is the word Vector or Velocity.

    I was mainly curious if i'm writing it wrong or not. I have a tendency to over complicate coding. Was curious if there was a simpler way to write C#. I guess not.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Because unity js hides it. It's still there, but hidden. It's better to not hide it, you will find your code becomes faster. In your example above, you are doing get component calls and constant accesses while in the c# version, at least having an intermediate means you can do all the work on that before finally assigning the result.

    It also leads to more readable code. Trust me when I say once you have made the switch, you will look back and think "what an idiot I was..." - it's one of those things.

    Don't forget that js and boo are slowly being phased out over time. Only 18% of Unity users even use Unity js now. C# is now the priority language for Unity. All the docs, all the energy, is being poured into C# first.

    This means it's a great thing you're making the switch.
     
    Caelorum likes this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    It shouldn't be a lot of added stuff. I never find myself making a copy, mutating the copy, and stuffing it back. (See below.)

    The key difference here is structs vs. classes. Check out the MSDN articles here and here. In the Unity framework, small types like Vector2, Vector3, and Quaternion are structs; other things are classes. (This is all pretty standard.)

    There generally is. In your example code, you're setting the X component of the velocity to 26. I can't imagine why you would do this in real code, unless what you really mean is to set the whole velocity to 26,0, in which case you could do:
    Code (CSharp):
    1.    rigidbody2D.velocity = new Vector2(26, 0);
    or
    Code (CSharp):
    1.    rigidbody2D.velocity = 26 * Vector2.right;
    If for some weird reason you did need to set the X component of the velocity to 26, while leaving the Y component whatever it was before, then you could
    Code (CSharp):
    1.    rigidbody2D.velocity = new Vector2(26, rigidbody2D.velocity.y);
    ...which is still a bit wordier than the JS version, but not so bad — and in actual practice, rarely needed anyway.

    HTH
    - Joe
     
    hippocoder and Caelorum like this.
  7. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    See your example of code is much nicer, that's the type of thing I feel I need to work on. I've been doing things on an error basis. I'll type in the javascript version, edit it to what looks like c#, then correct it based on errors.

    The reason I needed to set the x component and not the Y is because it's a pong based game, if I reset the Y every time the x slows down too much, the ball will never go up or down.

    And in C# if I try to use that first code I posted, it tells me to make a temporary variable. So I make one. Then it tells me I need to restate that rigidbody = the variable. Or the "copy' i guess. So I make that third line of code. It all ends up getting complicated and frustrating after a while. But i'm hoping eventually I'll adapt. Thanks to everyone who has been replying. It's all a painful learning process. If only I payed attention to all the boring "why" stuff in school. I was always too eager just to put the code into action, never cared why it did what it did.
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It does get easier. When I programmed Physynth and The Other Brothers, I used unity's JS. I came from a C++ background and felt these languages would be a piece of cake to migrate to Unity with. But I realised it was a false flag and not a good idea at all. C# turned out to be just as easy (once you learn what is needed) and for the most part it's identical code.

    All you need at this point, are new habits. Once you learn new habits, C# will be the same difficulty. Except you'll have so much more power, features and of course an entire internet of compatible C# code you can drop into your game.

    There is zero justification for the existence of Unity js given how close it is to C# but lacking most of C#'s extra power. It is quite simply, a mistake on Unity's part to ever invent and support this variation.
     
  9. Unity_Gangsta_Thug

    Unity_Gangsta_Thug

    Joined:
    Sep 26, 2014
    Posts:
    2
    Word son, it ain't even real Javascript. Unity need to keep it real.
     
  10. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    That's what i'm trying to figure out, is those habits. At the moment all I can really do is watch videos and tutorials, try to pick up bits of code, learn from my errors and try to build my own knowledge. It's those small habits. Having to go back to recheck if I'm using Vector right, look online to make sure i'm calling scripts from other GameObjects correctly, making my script much more simple than it is. Simple things like that. Like the code I posted up there, JoeStrout made it into one line. Things like that I'm just not good at yet.

    And The Other Brothers was an impressive looking game. Congrats on coding that.
     
  11. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    If you comes from a C++ background, you know what the heap and the stack are. Class/Struct is the answer to handle heap/stack pointer/variable without having to manually memalloc anything.

    Class => Heap
    Struct => Stack

    It's a bit more complicated than that, but it's the basic to remember.