GM:S 1.4 How to restart the script again?

Discussion in 'Programming' started by Edwin, Dec 6, 2018.

  1. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    144
    Hey, pals.

    I made a simple randomizing script with exception value, like if you want to get random value BUT, for example, without 100 or any value else.

    Code:
    /// irandom_exc(x, exc)
    
    // Arguments
    var max_value = argument[0];
    var exception = argument[1];
    
    // Choose random value
    var value = irandom(max_value);
    
    /*
        Here's the problem. I want to check if value equals to exception and if it DOES,
        I want to make it restart the script so it can randomize it again
    */
    
    if (value == exception) {
        // RESTART THE SCRIPT
    } else {
        return value;
    }
    
    How I can perform it, please help!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,241
    Don't repeat the whole script, repeat just the randomizing part.
    Code:
    var max_value = argument0;
    var exception = argument1;
    var value;
    do {
      value = irandom(max_value);
    } until (value != exception)
    return value;
    
    You should familiarize yourself with all the control structures on this Manual entry, almost all of which GML shares with other curly-brace languages except with and do-until (most others have do-while). GML is NOT a goto language and neither are most high-level languages in current use, so stop thinking in terms of jumping to arbitrary lines.
     
    Edwin likes this.
  3. Evan Malinowski

    Evan Malinowski Member

    Joined:
    Apr 17, 2018
    Posts:
    207
    Code:
    if (value == exception) {
        // RESTART THE SCRIPT
    } else {
        return value;
    }
    You pretty much answered yourself, just plug in your code
     
  4. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    144
    Oh, I forgot about do until. Thanks a lot!
     
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    661
    Any loop could have done it. But I'd be careful with @Evan Malinowski's suggestion. GM doesn't necessarily play well with recursion and there's a limit to the number of recursive calls you can make. I think the script ends up just returning 0 or something if the recursive limit is hit.
     

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