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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

if() return in Update Function

Discussion in 'Scripting' started by Salazar, Jul 28, 2015.

  1. Salazar

    Salazar

    Joined:
    Sep 2, 2013
    Posts:
    235
    Hello people,

    I couldnt figure out what this piece of code actually doing in one frame. (Especially "return")
    I just want to know, what "return" keyword doing here.

    Code (JavaScript):
    1. function Update()
    2.   {
    3.   if ( Input.touches.Length == 0 ) return;
    4.   yellowRay = theCam.ScreenPointToRay( Input.touches[0].position );
    5.   if ( ! collider.Raycast (yellowRay, theHit, 10.0 ) ) return;
    6.   onTHINGPointEnd = theHit.point;
    7.   if ( Input.touches[0].phase != TouchPhase.Moved ) return;
    8.   startedAtGlasswise = Input.touches[0].position - Input.touches[0].deltaPosition;
    9.   yellowRay = theCam.ScreenPointToRay( startedAtGlasswise );
    10.   onTHINGPointBegin = onTHINGPointEnd; // our default political position
    11.   if ( collider.Raycast (yellowRay, theHit, 10.0 ) )
    12.   onTHINGPointBegin = theHit.point;
    13.   if ( startedAtGlasswise.x < 0.1 * Screen.width ) return;
    14.   if ( Input.touches[0].deltaTime == 0.0 ) return; // just in case
    15.   moveLikeThisInRealWorld( (onTHINGPointEnd - onTHINGPointBegin) / Input.touches[0].deltaTime );
    16.   }
    17. function moveLikeThisInRealWorld( dd:Vector3 )
    18.   {
    19.   }
     
  2. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    That's very poorly formatted code but in general an empty return is just that, it escapes the rest of the conditional and returns to the normal code flow outside of the conditional.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Return does not escape a conditional, it escapes a function. Using it inside of the Update function will exit the Update right there, meaning nothing else in the function will run that frame. I know this might feel like nitpicking terminology, but it's important.

    You might also be interested in the break keyword, which does the same thing for a loop (for, foreach, do, do-while). That doesn't escape a conditional either, unless you just happen to be inside of a conditional when you break the loop it's inside of (just FYI).

    It's worth noting that a 3 second google search for "return c#" or "return javascript" would've brought you to the MSND Reference explaining exactly what it does. You don't really have to post EVERYTHING on the support forum- especially not fundamental programming language stuff.
     
    Last edited: Jul 28, 2015
    Kayckbr, Liu_Huimin, LeFuji and 6 others like this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It's a way to get out of a function early if certain conditions are not met. In this case the line says. "Stop processing if there are no touches".
     
    Deleted User, LeFuji and Salazar like this.
  5. Salazar

    Salazar

    Joined:
    Sep 2, 2013
    Posts:
    235
    @Lysander
    Thanks for the answer. I really looked up forum search, google, msdn documentation, I couldnt find a plain answer like yours(Really appreciated!), at least this type of usage with unity. Hope this thread helps others like me..

    Regards,