Coffee-Break Tutorials: Pausing Your Game (DnD™)
Hello again and welcome to another coffee-break tutorial! This series offers quick and easy tutorials that can be implemented in just a few minutes, with the idea being to teach you some basic coding practices using GameMaker Studio 2 that will be useful to you in your own projects. Today's subject is how to pause your games! Note that this tech blog is for Drag and Drop™ users, so if you prefer GML, you should follow the companion tech blog that we've prepared here.
Before continuing, this tutorial uses the finished Space Rocks tutorial project as it's base, so if you're new to GameMaker Studio 2 you should go through that before doing this, although you can download the finished tutorial project from the link below and use that too if you wish:
If you download the above project, you need to open GameMaker Studio 2 and select "Import" on the Start Page to open it, or simply drag it onto the IDE and drop it, which will show you a prompt to import the project.
SETTING UP
There are various different ways that you can pause a game, but for this tutorial we're going to use one of the simplest, which is to deactivate all the game instances and then draw a "Pause" screen. Deactivated instances still exist in the game, but they're in a kind of "limbo" where they do nothing - they won't draw and they won't run their events. This makes deactivating things ideal for creating a pause system, although it does create a minor issue... since deactivated instances don't draw, the screen goes blank! However, as you'll see, we can fix that!
To get things started, you need to open up the object
obj_Game
The variable
paused
true
false
paused_surf
SETTING PAUSED / UNPAUSED
We now need to open up the Step Event of the object
obj_Game
We'll be using the "P" key to pause the game in this example, but you can use any key really - like escape - or have a button that detects a mouse click or even detect a gamepad button instead.
The DnD™ code to add into this event is:
So, here we're checking to make sure we're in the game room, then we check to see if the "P" key is pressed. If it is pressed, we toggle the
paused
not
!
false
paused_surf
The next bit of code in this event is for dealing with the alarms in the controller object. Alarms count down by 1 at the beginning of every step of the game, which means to pause them, we simply add 1 every step while the game is paused! So, we are checking to see if
paused
true
DRAWING THE PAUSE SCREEN
The final thing we need to do now is in the Draw Event of the
obj_Game
case
rm_Game
We'll break the code to add into two sections to make it easier to follow. To start with, we'll have this code:
Here we are checking to see if the
paused
true
paused_surf
surface_exists()
false
paused_surf
Next we create our surface and draw the application_surface
application_surface
The final bit of code to add goes underneath what you've just added at the position shown:
We'll be adding an
else
Here all that we're doing is drawing the pause surface to the screen, then drawing a low alpha black rectangle over it to make it look "faded", before finally drawing the text "PAUSED" to let the player know that they've paused the game. Really, you code draw anything here, but this is the simplest thing to do and suits this quick tutorial.
You can run the game now, and in the main game room press the "P" key to pause and unpause the game.
SUMMARY
That's it for this tutorial! The game has a pause screen, and the code is simple enough that you should be able to add it into your own projects and modify it to suit your needs. Some things you can try that I like to do in my own games are:
lower the game music volume a bit when the game is paused, and return to full volume when unpaused
add in menu buttons to restart the game, go back to the start screen or quit to desktop (for example)
add in some graphic effect when pausing, like gradually dim the surface being drawn, or - if you're feeling brave! - apply a shader to the surface to blur it slightly (I love this effect and use it in my own projects)
Thanks for reading and we hope that this has been interesting and useful to you! If you'd like to see the finished code in the tutorial project without having to actually add it all yourself, or you want to compare what you have with something, you can find download the file below which contains everything we've discussed here.
Happy GameMaking!