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

how to exit code but not the whole Event?

For a long time I was puzzled by why an enemy object's Create event wouldn't work (it would only play actions up until a certain point). Then I realized that in a code that is played in the Create Event, when certain conditions are met, it says the 'exit' statement; I originally meant for it to exit the code, but 'exit' statement cancels the whole event, am I right? So any actions in Create Event after the code will never play?
How can I only end the code, and not the entire event?
 

rytan451

Member
What do you mean by exit the code? If you mean "exit the if statement", you don't need an exit statement to exit an if statement if you've reached the end of the code.
 

Roleybob

Member
Some types of code blocks you can use break; to exit the loop but not the event - for loops, repeat loops, switches, with statements, etc.

Or you might be refering back to older versions of GM - I'm pretty sure you used to be able to add several separate code blocks to an event and GM would only end the current one when exit was used. These days, all code in an event is run as one code so exit will end the entire event for that instance
 
Last edited:

woods

Member
Then I realized that in a code that is played in the Create Event, when certain conditions are met, it says the 'exit' statement;
the create event is only ran once
vs
the step event runs the entire code n times per second based on room speed

you can use break: to exit a loop, but the code runs full thru each step(i think)
 
here's the code:
GML:
if wandering=true{
if instance_exists(obj_cpu_wander_marker){
alarm[7] = room_speed * 3;
max_dist = 800; // The distance from the instance to set the point to
if distance_to_object(instance_nearest(x,y,obj_cpu_wander_marker)) >= max_dist{
var _dir = random(360); // Get a random direction for the point
xgoto = x + lengthdir_x(max_dist, _dir); // Set the point position
ygoto = y + lengthdir_y(max_dist, _dir);}}}

if wandering=true{
if instance_exists(obj_cpu_wander_marker){
alarm[7] = room_speed * 3;
max_dist = 800; // The distance from the instance to set the point to
if distance_to_object(instance_nearest(x,y,obj_cpu_wander_marker)) < max_dist{
exit;}}}

if wandering=true{
if !instance_exists(obj_cpu_wander_marker){
alarm[7] = room_speed * 3;
max_dist = 500; // The distance from the instance to set the point to
var _dir = random(360); // Get a random direction for the point
xgoto = x + lengthdir_x(max_dist, _dir); // Set the point position
ygoto = y + lengthdir_y(max_dist, _dir);}}
in the middle section, my idea was that if there is an instance of 'obj_cpu_wander_marker' near enough, then this piece of code would end there. However, the 'exit' I used there apparently ended the whole Event where this code was put. If there are any Actions in Create Event after this code, then they won't run after the 'exit' statement plays. So is there any other statement that can be used to just end this code there, and resume to play the other Actions in the Create event?
Does 'end' or 'break' do that?
 
Ah yup. this just from the Manual:

exit;
'if you use this event in a code block from within an object, it will exit the entire event even if there are various separate code blocks after the function has been called. '

Dammit.. šŸ¤£

I basically want to have an action that could end that particular code, not the whole event. so 'exit' won't do...
 

woods

Member
else
if the first bit of the if statement is able to do its thing, the else bit is essentially a break: right?

if (this is true)
{
do this
}
else
{
do that
}

====

looking at potential direction your game could go..
possibly look into using a state machine
 
I literally have no idea what that code does. I don't think it's going to do what you think it's going to do either. Can you explain in plain english what you are trying to achieve? Are you trying to find a position to place a wandering marker? Are you trying to create a line of wandering markers until the max distance has been reached?
 

Roleybob

Member
So you want the 3rd block to execute if the marker is further away from the vcalling instance but not if it is nearer?

Additionally:

- Why are you evaluating this 3 times when it's value cannot change in any of the code?
Code:
if wandering=true
- Use == to evaluate conditionals. = is for setting the value of a variable

- To check for true, you can just use
Code:
if (wandering)
and to check for false you can use
Code:
if !(wandering)
- It's your code so you can lay it out how you like but I find it hard to read. IT would be much easier to read if you used more whitespace
 

woods

Member
is this the super stripped down version you are trying to get at?

Code:
if instance_exists(obj_cpu_wander_marker)
{
max_dist = 800;  // increase the range the npc can travel...make him wander farther than base range
}
else
{
max_dist = 500; // base range for npc
}

reset timer
 
I literally have no idea what that code does. I don't think it's going to do what you think it's going to do either. Can you explain in plain english what you are trying to achieve? Are you trying to find a position to place a wandering marker? Are you trying to create a line of wandering markers until the max distance has been reached?
yeah, its complex, but it works!
its a movement code for a certain enemy type. its supposed to wander around, until the player comes near and it attacks. the wander marker-objects are objects that draw the enemy towards them (when needed); otherwise it just wanders around with this code.
I wanted to know if there was a way to cancel just the code and not the whole Event like 'exit' apparently does
 
the code is big but its worked fine, I was just puzzled until I realized that exit; will cancel the whole Event the code is in, not just the code.
I actually deleted the whole middle section of the code (with the exit; statement) altogether, technically it wasn't needed. But don't bother with the code itself, theres been no issues with that until now.
But still, my original question remains...
 
The reason why people are commenting on the code itself is that it's structured wrong for what you are trying to do. That's why people keep mentioning loops or switch statements. Those are conditionals you can break out of. If you want something to be checked UNTIL something happens, then you use a loop. Stringing together if statements that are all checking the exact same condition is completely redundant. Using this method to make an enemy move is very rube goldberg-esque.
 

FrostyCat

Redemption Seeker
GML is not a goto language. You're trying to design your code around a language feature that doesn't exist, and rookies routinely drive themselves crazy doing this.

What you need is an else block and a refactor, not "a way to skip part of an event". You should also look for ways to prevent or eliminate no-ops, not ways to create them and give them power over unrelated code.
GML:
if (wandering) {
    alarm[7] = room_speed*3;
    var _set_goto = false;
    if (instance_exists(obj_cpu_wander_marker)) {
        max_dist = 800;
        if (distance_to_object(instance_nearest(x,y,obj_cpu_wander_marker)) >= max_dist) {
            _set_goto = true;
        }
    } else {
        max_dist = 500;
        _set_goto = true;
    }
    if (_set_goto) {
        max_dist = 500;
        var _dir = random(360); // Get a random direction for the point
        xgoto = x + lengthdir_x(max_dist, _dir); // Set the point position
        ygoto = y + lengthdir_y(max_dist, _dir);
    }
}
 
thanks for all your responses.
from what I gathered, such an action doesn't exist: one that is only purposed to cancel the current code block that is playing.
I understand there are better ways to do the movement code. For now, I managed to simplify it a bit by disabling the middle section altogether.
 

Nidoking

Member
You can use an if block to make code run only if you want it to. I can't imagine how you could possibly want or need anything more complex than that.
 
Top