Simple GameMaker performance throttling

Here’s a quick tip for performance throttling in GameMaker.

Say you’ve got some code that you need to execute frequently, but if the game starts slowing down too much, you can live without executing this block of code.

Like, for example, say you want to spawn a new instance of some object very frequently, such as in the Step event, but if performance starts to lag you can skip it. You could try to test out how many instances the game can handle without frame rate dropping to an unacceptable level, and cap the number to something somewhat below the maximum. The problem is that this number will vary depending on the hardware. Someone running your game on an older, slower machine will not be able to sustain the same performance that someone with a brand new, high end machine. There really isn’t a true, one-size-fits-all number that works for every situation.

What you really want to do is base the cap on the current performance of the game as it’s running right now. To do that, wrap it up in an if statement like this:

if room_speed < fps
{
 //keep doing the thing that will eventually cause performance issues
}

The way this code works, as soon as fps drops below room_speed, it will stop doing the thing that contributes to the performance problem. This technique does not guarantee that fps will never drop below room_speed, but it will cause performance to stop degrading by not contributing to the problem once performance has degraded to the point that the conditional check takes the “false” branch.

If you don’t want ANY noticeable performance degradation, you may want to make the conditional check be something more like

if (room_speed + 10) < fps

instead; this will give you a little buffer to keep the fps enough above room_speed that you should not see any noticeable performance problems. Or, substitute the calculation room_speed + n with the literal value that you don’t want fps to drop below. Use this to ensure a safer margin of acceptable GameMaker performance.

3 Comments

Add a Comment
  1. Great article – as always!

    just FYI, I found one small typo:

    if (room_speed – 10 < fps)

    :)

      

    1. Actually, that’s not a typo. The intent of the conditional is to check whether fps has dropped below an acceptable value. If we want to have some “extra” fps to cushion performance so that fps never comes close to room_speed, then the check needs to be if ((room_speed + padding) < fps). If we checked room_speed - padding < fps, then we wouldn't begin throttling performance until it had already degraded performance to below room_speed - the padding value -- which would be quite noticeable, and not desirable.

        

      1. Okay, sorry. I slightly misunderstood the script and thought that for example a frame drop from 60fps to 50fps would still be okay and not noticeable that much and thus, the script would only stop doing things that cause performance issues when “fps” is smaller than “room_speed – n”. Sorry and thanks for the clarification.

          

Leave a Reply