GML Blog‎ > ‎

Pausing in Game Maker with Game Loops

If you have any experience making games with languages like java or c++, then you know what I mean when I say that you need to code a game loop for them. If your solely a Game Maker programmer, then you still know what a game loop is, it's just that you never knew that it was called that. The step event in Game Maker is a game loop. A game loop is basically like a for loop but with a set amount of time that it must use up. Most game loops are structured like this:

  • Game loop start---

  • Save very precise time to a variable (mili-nano-”really tiny”- seconds)

  • Do everything here (Step event actions)

  • Subtract saved time from current time. This is how long it took to perform the above. If it took less than a 60th of a second (Or whatever the room speed is set to) then wait out the remainder of the time.

  • Game loop repeat---

When comparing the way GML does game loops to the way that other languages do them (Or rather their lack of built in game loops) you start to notice how much better these other languages are at controlling the game loop (Duh, that's because you have to code them yourself). You may be thinking that Game Maker has lost the game loop battle but alas, there is a victory for GM waiting on the horizon. In this article, I will show you how to control Game Maker's Step Event with the precision of a very accurate, precise, exacting, ...thing.

You probably know about user events by now. It's a custom event that any object may have. You can have up to 16 user events per object (0-15). These will be your custom game loops. You can make game loop layers just like Game Maker has (Begin Step, Step, and End Step). Try to designate a specific number of user events that will be used for various game loops in your game. ev_user0 could be for game logic/physics, ev_user1 could be for the GUI (Graphical user interface), and ev_user2-7 could be designated for additional uses later on. This leaves you with 8 ev_users that you can use for things besides game loops. Now, how do you trigger these user events? Easy, make a controller object that executes the following code in its step event:

with (all) event_perform(ev_other,ev_user0)

This code will have every object perform their first user event every step of the game. If an object doesn't have a user event, or only utilizes other game loops, the code simply skips over that object.

The beauty of this system is that if you want a pause feature, you simply have to make a conditional for the game loop that controls logic/physics etc..., but keep the GUI events going. Finaly, a pause feature for Game Maker!  You can pause the game, alter some settings in the pause menu, and continue playing. You can even control how often a game loop executes. If you separate the enemy game loop from the protagonist game loop, you could add a crude slow motion feature (I discourage this though because the enemies movements will appear jerky. There are better ways to implement slow motion but that's a whole other article in and of itself.).

It is a good idea to implement this trick in the beginning of your development cycle, but it can be worthwhile to shoehorn it into an existing project.


Affiliates