Search Unity

goto command

Discussion in 'Scripting' started by Andy, Apr 1, 2007.

  1. Andy

    Andy

    Joined:
    Nov 5, 2006
    Posts:
    50
    Is there a goto command in JavaScript. I did some searching an it does not appear to be part of the JavaScript language. Is there a way to do that in JS?

    Thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm assuming this is an April Fools joke. :)

    --Eric
     
  3. Andy

    Andy

    Joined:
    Nov 5, 2006
    Posts:
    50
    Well, Eric sadly I have to report that it is not a joke. Also I should mention that I am a beginner programmer, beginner JSer, and beginner game designer. :oops: What I am trying to do is this: I have a long script and I would like to jump to the end and not execute a large chunk of it, in other words just skip a chunk of the script if some condition is met. It seems that a "goto" like command might do something like this.

    Tks
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    OK, sorry...you know what they say about assuming. :) Anyway, no goto command. It's very 1970s. And much horribleness arises from it....

    Is there any reason why you can't use an if statement?

    Code (csharp):
    1.  
    2. if (!someCondition) {
    3.    //do stuff here
    4. }
    5.  
    That way the stuff in the if statement is only run if someCondition isn't true. (Equivalent to writing "if (someCondition == false)") But you probably already know about that. You can also use "return" to end a function early:

    Code (csharp):
    1.  
    2. function doSomeStuff () {
    3.    // do some stuff here
    4.    if (someCondition) {
    5.       return;
    6.    }
    7.    // do more stuff here
    8. }
    9.  
    The return means the function is ended early at that point and "do more stuff here" is never executed. Or you can use a while loop:

    Code (csharp):
    1.  
    2. while (!someCondition) {
    3.   // do stuff here
    4. }
    5.  
    That way the loop will run only as long as someCondition is false, and stops when it turns true.

    There are various methods of program flow control; that's just a few. Hard to say what would work best without seeing the code in question, but hopefully that helps some.

    --Eric
     
  5. Andy

    Andy

    Joined:
    Nov 5, 2006
    Posts:
    50
    The return command seems to work now. It seems to stop not only the function but the whole script from executing. When I tried it a few day ago it didn't seem to work. Probably something wrong I did in the script. Thanks for your assistance.




    Oh by the way go outside the moon is no longer visible in the sky. The news reports say that it has left its orbit around the Earth.
     
  6. Andy

    Andy

    Joined:
    Nov 5, 2006
    Posts:
    50
    OK the moon comment was an April fools joke. Don't go outside.

    But seriously. I do have one more question about the return command. Is it designed to stop the function that it is located in, or does it stop the rest of the script from executing?

    Thanks
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Too late! I ran outside in a panic, yelling and screaming all the way, but it was still there...hmmm....

    Just the function. I've used it, and that's the way it works. Other functions should run as normal, unless the return prevents them from being called of course.

    --Eric
     
  8. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    To run with this theme, is there a break command for loops?
     
  9. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Yes. break will exit the current loop and continue will jump back to the start of the loop without evaluating the loop expression (ie. it will restart the current iteration).

    Note though that the current implementation does not support labeling loops, so you can currently only exit the innermost loop.
     
  10. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    That's not completly correct, continue will reevaluate the loop (as well as increment in the case of foreach). Continue doesn't restart the iteration, it continues to the next.