Legacy GM How to restart the script again?

E

Edwin

Guest
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!
 

FrostyCat

Redemption Seeker
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.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
Code:
if (value == exception) {
    // RESTART THE SCRIPT
} else {
    return value;
}
You pretty much answered yourself, just plug in your code
 
E

Edwin

Guest
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.
Oh, I forgot about do until. Thanks a lot!
 
Any loop could have done it. But I'd be careful with @EvanSki'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.
 
Last edited by a moderator:
Top