SOLVED "exit" structure exiting too far?

Spring

Member
Hello all, I was testing the exit features, but it seems like the exit feature does not quite work the way I expect it to, see below:

GML:
if keyboard_check_pressed(ord("E")){
    { //this is the beginning of the code block that I expected "exit" to close
        cs_logAddEntry("exit test has begun.");
        if keyboard_check(ord("M")){exit;}
        cs_logAddEntry("if you had been hold [M], this line shouldn't print");
    }  //this is where I expected "exit" to bring me to.
   
    cs_logAddEntry("exit test has ended."); //when the M key was held, this line and all below it does not run
}
Does anyone know how I can make exit only close/skip the part of the code I want to?
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Exit will immediately end the event, method or function. In this case it looks like it's in the Step event so calling it here will end the event immediately, and no code that is after the exit call will be run. Check the manual for how the command works: https://manual.yoyogames.com/GameMaker_Language/GML_Overview/Language_Features/exit.htm?rhsearch=exit&rhhlterm=exit exits

Just modify your code like this so you don't need exit:

GML:
if !keyboard_check(ord("M"))
    {
    cs_logAddEntry("if you had been hold [M], this line shouldn't print");
    }
 

Roldy

Member
Hello all, I was testing the exit features, but it seems like the exit feature does not quite work the way I expect it to, see below:

GML:
if keyboard_check_pressed(ord("E")){
    { //this is the beginning of the code block that I expected "exit" to close
        cs_logAddEntry("exit test has begun.");
        if keyboard_check(ord("M")){exit;}
        cs_logAddEntry("if you had been hold [M], this line shouldn't print");
    }  //this is where I expected "exit" to bring me to.

    cs_logAddEntry("exit test has ended."); //when the M key was held, this line and all below it does not run
}
Does anyone know how I can make exit only close/skip the part of the code I want to?
As @Nocturne stated you would accomplish 'exiting' the code by structuring you conditionals properly. The 'exit' keyword behaves similar to 'return.'

I believe you are looking more for the functionality of break and continue style keywords. However those only apply to for, repeat, while, do iteration loops, switch statements and the with keyword.
 

Nidoking

Member
For a laugh, you can get break functionality without a proper loop like this:

GML:
do
{
  //some stuff you want done
  break;
  //some stuff you don't want
}
until (true);
This is actually pretty close to the way Java handles certain language features.
 
Top