• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Is there a GML equivalent of 'jump'/'goto'/'loop'?

R

Rosieu

Guest
Hi again Everyone!

I was wondering if anyone knew of an expression in GML that acts in the same way as expressions such as 'jump' or 'loop' do in other frameworks/languages? Basically I'm wondering if there is a way to control the flow of code within a script / execute code action.

The code below is causing some errors:
Code:
if (num2 != card1)
{
    card2 = num2;
}
else
{
    num2 = irandom(ds_list_size(global.playdeck));
}
because if the 'else' is triggered, even though 'num2' is regenerated, it isn't stored in 'card2'. It could easily be fixed by me including 'car2 = num2' in the else event also, however it wouldn't run the if statement again, which is what I'm looking to do.

if I were to write what I want to do in 'pseudo code' it would look something like this:
Code:
if (num2 != card1)
{
    card2 = num2;
}
else
{
    num2 = irandom(ds_list_size(global.playdeck));
    jump_to(0)
}
Where (0) would be the first line of the snippet (in this case the beginning of the boolean check.


Sorry this question is poorly put together, I'm trying to avoid seeming like I'm looking for someone to fix my code, but it's so hard to articulate without using examples from my code!

Anyway, thanks in advance for any help anyone can give!

Best wishes,
Rosieu
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
GameMaker does not employ a conventional "goto" statement (partially for reason of compiling to JS, which does not either). In most cases it is easily replaced by loops of one or other kind, however,
Code:
while (num2 == card1) {
    num2 = irandom(ds_list_size(global.playdeck));
}
card2 = num2;
 
R

Rosieu

Guest
Oh! I hadn't considered using a 'while' loop like that! I've only recently started exploring loops in GML and I'm still getting the hand of it!

Thank you so much for your help, that is great to know you can use 'while' loops that way! :D

Thanks again!

Kind regards,
Rosieu
 

TheouAegis

Member
the other method I used is to have a giant switch governing the whole script and whatever argument you passed to the script determines which case to run. The only drawback to this is you can potentially cause a stack overflow if for whatever reason you recursively called the script enough times. this was the method I used in a project which I had to make run in the Lite version of game maker 8. there was a limit on the number of scripts I could use, so I wrote it to use only 1.
 

zbox

Member
GMC Elder
Not sure if relevant, but If I am coding sloppily and I need some quick way to break out inline but can't use return or exit , I just wrap it in some form of a single-execution loop:
Code:
do {

statement;

if (condition)
    break;

statement;
statement;
} until false;
Probably don't do this though, or use goto.. It's bad. But sometimes sneakily it comes in useful.
 
Top