Discussion [Suggestion] Nested break

Discussion in 'GameMaker Studio 2 Community Tech Support' started by gnysek, Jan 19, 2018.

  1. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,304
    Currently, in GM you need to write:
    Code:
    var _found = false;
    for(var i=0; i<10; i++) {
        for (var j=0; j<10; j++) {
            if (a[i, j] == 5) {
             _found = true;
             break;
          }
        }
        if (_found) break;
    }
    Some other languages, allow to put number to exit a number of for-loops:
    Code:
    for(var i=0; i<10; i++) {
        for (var j=0; j<10; j++) {
            if (a[i, j] == 5) break 2;
        }
    }
    It would be a nice addition to GML syntax.
    Code:
    break N; // goes out from N loops, skipping further actions
     
  2. Octopus_Tophat

    Octopus_Tophat Member

    Joined:
    Oct 2, 2016
    Posts:
    94
    I have a bunch of these "break-controlling temp vars" in my code, so I totally love this idea.
     
    Bentley and gnysek like this.
  3. xot

    xot GMLscripter GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    113
    This is what goto is for.
     
  4. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,317
    Better than break N would be the label syntax like in java.
    Code:
    outer: for(...) {
      for(...) {
        break outer;
       }
    }
    
     
  5. xot

    xot GMLscripter GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    113
    That's not bad. JavaScript and Go work that way too. I definitely prefer labels over some literal depth value.
     
  6. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,266
    Yeah I like the label syntax too, that would be a nice addition.
     
  7. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    219
    I am just here to vote no on the goto command and spaghetti code
     
  8. xot

    xot GMLscripter GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    113
    Spaghetti is delicious! Change my mind.
     
  9. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    108
    Some people say you should never use break or continue. I disagree and use them fairly often, but I think that if you are at the point where you need such noisy features as multiple-level breaks then you should focus more on refactoring your code. You can make a very small change to the OP example to make it not require breaks at all, for example:

    Code:
    var _found = false;
    for(var i = 0; i < 10 and !_found; i++) {
        for(var j = 0; j < 10 and !_found; j++) {
            _found = (a[i, j] == 5);
        }
    }
    
     
    xot likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice