Search Unity

Changing C# into JS

Discussion in 'Scripting' started by Deleted User, Oct 8, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have been changing my code from C# into JS (job mentorship in web development). At the moment I am off on my playerscript.js.
    Code (JavaScript):
    1.     var float: speed = 0.1F;
    2.     var myInt : Score = 0;
    3.     var myInt : HighScore = 0;
    4.     var myint : Lives = 5;
    5.     var ExplosionPrefab : Transform;
    6.  
    7.  
    8. // Use this for initialization
    9. function Start () {
    10.     HighScore = PlayerPrefs.GetInt("HighScore");
    11. }
    12.  
    13. // Update is called once per frame
    14. function Update () {
    15.  
    16.     //movement speed of ship
    17.         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    18.             var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
    19.             transform.Translate(-touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
    20.  
    21.                 transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    22.  
    23.             //screen boundary
    24.             var distance = (transform.position - Camera.main.transform.position).z;
    25.  
    26.             var leftBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x + (playerSize.x/2);
    27.             var rightBorder = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x - (playerSize.x/2);
    28.             var bottomBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y + (playerSize.y/2);
    29.             var topBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y - (playerSize.y/2);
    30.  
    31.             transform.position = (new Vector3 (
    32.                 Mathf.Clamp (transform.position.x, leftBorder, rightBorder),
    33.                 Mathf.Clamp (transform.position.y, bottomBorder, topBorder),
    34.                 transform.position.z)
    35.                                   );
    36.    
    37.                 }
    38.                 if (Score > HighScore)
    39.                 // set HighScore from current is higher
    40.                 HighScore = Score;
    41.                 // To save it
    42.                 PlayerPrefs.SetInt("HighScore", HighScore);
    43.                
    44.  
    45.                 void OnGUI()            //error
    46.                 {
    47.                 GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(3, 3, 3));
    48.                 GUI.Label (new Rect(10, 10, 120, 20), "Score: " + playerScript.Score.ToString ());
    49.                 GUI.Label (new Rect(10, 30, 120, 20), "HighScore: " + playerScript.HighScore.ToString ());
    50.                 GUI.Label (new Rect(10, 50, 120, 20), "Lives: " + playerScript.Lives.ToString ());
    51.             }
    52.             void OnTriggerEnter2D(Collider2D)            //error
    53.             {
    54.                 playerScript.Lives--;
    55.                 var other : ShipScript = gameObject.GetComponent("player");
    56.  
    57.                 StartCoroutine(DestroyShip());
    58.             }
    59.  
    60.             IEnumerator DestroyShip()        //error
    61.             {
    62.                 Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
    63.                 gameObject.renderer.enabled = false;
    64.                 transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    65.                 yield WaitForSeconds(0.1f);
    66.                 if (playerScript.Lives > -1)
    67.                     gameObject.renderer.enabled = true;
    68.                 else
    69.                     Application.LoadLevel("gameover");
    70.             }          
    71.             }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    no offense... but... why?

    I mean, I can get writing unityscript. But why take already existing C# code and convert it to unityscrypt?

    As for your errors, they're happening where you're declaring functions. In C# your declare them as:

    accessModifier returnType name(paramType paramName)
    Code (csharp):
    1.  
    2. public int SomeFunc(bool inputValue)
    3.  
    in UnityScript/Javascript it is:

    accessModifier function name(paramName:paramType) : returnType
    Code (csharp):
    1.  
    2. public function SomeFunc(inputValue:bool) : int
    3.  
    AccessModifier isn't required in either, if you don't include it, private is implied for C#, and I think public is implied for unityscript. Not sure for sure on the US/JS side of that.

    ReturnType isn't required for US/JS. You don't include it at all if there is NO return type (void in C#), and otherwise, the compiler will figure it out on its own. Although, that does make it less readable, so I always have put return types in no matter what.


    so
    Code (csharp):
    1.  
    2. void OnGUI()
    3.  
    becomes
    Code (csharp):
    1.  
    2. function OnGUI()
    3.  

    Also, you have your class fields all messed up with:

    Code (csharp):
    1.  
    2. var float : speed = 0.1f;
    3. var myInt : Score = 0;
    4. var myInt : HighScore = 0;
    5. var myInt : Lives = 5;
    6. var ExplosionPrefab : Transform;
    7.  
    variable declaration in US/JS is:

    var variableName:variableType = value;

    'speed' is not a type, float is. So you should probably reverse those.

    you declare multiple variables 'myInt' and typed what appear to be names. I would say reverse those, but 'myInt' isn't a type either, but I assume that was supposed to be 'int'.

    ExplosionPrefab looks right.

    I have no idea how you got 3 completely different syntactical organization all in 5 lines of code. Programming languages are designed to be consistent, not arbitrary.
     
    Last edited: Oct 8, 2015
    Kiwasi and Deleted User like this.
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should realize that "JS" in Unity is not Javascript, and is typically referred to as Unityscript to reflect that.

    Yes, the default in Unityscript is public.

    You can specify void if you like; it's just another type. In most cases it's not necessary to declare the return type, void or otherwise, if the compiler can figure it out, but in certain cases it can't (e.g. recursion), so in that case you'll get an error message, and you'll have to supply the type in order for the code to compile.

    --Eric
     
    Kiwasi likes this.
  4. Deleted User

    Deleted User

    Guest

    Thanks a lot. Yes, I know it is UnityScript and I Told my mentor why I stopped since it's not web development javascript. I just want to get it done with and work on what I have now.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it's a little concerning that the so called mentor doesn't understand the difference between programming and web development. I believe the closest unity gets to be "web development" is a web player build target and pinching variables from the web page it's embedded into. :confused::eek:
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Depends on your definition of Web development. Especially back-end web development is as pure programming as Unity is. And even some Javascript frameworks are more full blown programming frameworks then scripting frameworks (AngularJS, Knouckout etc) And of course you have typescript nowadays which allows normal Javascript to be developed close to how C# works.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    WebGL builds?
     
  8. Deleted User

    Deleted User

    Guest

    he just said it so I could learn more than one language- but I know html and css. Meh....anyways- thank you, guys.