Short Circuit Evaluation

Recently there was a change made to the way that GameMaker: Studio handles “if” evaluations. This change was to introduce short circuit evaluation. Those of you who come from a programming background may know what this means but those of you who are just starting with GameMaker: Studio, or new to programming, won't, and so before continuing, let's just look at how GameMaker: Studio used to handle things...

The Old Way

When you want to evaluate values, you usually do it with an “if” like this:

var val = irandom(9);
if (val < 5)
{
//do one thing...
}
else
{
//do another thing
}

And when you want to evaluate more than one value you would then have something more like this:

var val1 = irandom(9);
var val2 = irandom(9);
if ((val1 < 5) && (val2 > 4))
{
//do one thing...
}
else
{
//do another thing
}

Now, previously when using GameMaker: Studio, that “if” would evaluate both parts, checking the value of val1 and val2, even if val1 had evaluated as false. This is obviously not optimal, since sometimes you may want to evaluate several values, and those values could be derived from expensive functions etc... which will make them more costly to run.

This situation led to people using “nested” ifs, where each evaluation was given it's own “if”, and, although that works, it's still not optimal for you as it takes longer to write out, may be confusing to read over, and you may also need to add repeated “else” to cover each nested “if”. For that reason the developers have changed to short circuit evaluation.

The New Way

Since the old way required you to either add extra code or make your game perform multiple (and pointless) evaluations, it was decided to implement short circuit evaluation. This simply means that the moment any evaluation of a value returns false, no other evaluation is done for that line of code, since it makes no difference to the outcome. For example, in our example code above “val2” will not be evaluated if "val1" is not less than 5.

Now, this may seem like a minor change to some of you, but to those of us used to working with GameMaker: Studio for some time now, it comes as a welcome optimisation. It means that you can write more compact code without worrying about a performance hot for one thing, and it also means that you can place complex function evaluations at the end of the “chain” so that it is only run if absolutely necessary. Consider the following:

var inst = instance_nearest(x, y, object0);
if (instance_exists(inst) && (point_distance(inst.x, inst.y, x, y) > 100))
{
show_debug_message("OBJECT FOUND");
}
else
{
show_debug_message("NO OBJECT!");
}

Previously, no matter what the instance_exists() function returned, the point_distance() function would have been evaluated... and it would have given an error if no instance was found, since both evaluations were carried out, even though instance_exists() returned false.

Now however this is not the case, and a return value of “false” for instance_exists() would break (or “short circuit”) that evaluation chain, causing the “else” code block to run.

Switching It On And Off

Since this is a new feature, and since we are not saure of the impact it may have, there is an option in the Global Game Settings to switch this feature on and off.

General_Tab.png

If you are importing legacy GameMaker files (gmk, gm81 etc...) then this will be flagged as off, while all new projects started within GameMaker: Studio will have it flagged as on. However it is worth noting that it really shouldn't break anything in your games (even legacy ones), and will definitely benefit them.

Have more questions? Submit a request

0 Comments

Article is closed for comments.