Adding the ability to pause is an easy way to polish your game. It make your game feel more like a “real” game. Luckily GameMaker makes pausing games very easy.
There are two major ways to pause your game. One is to create a globalvar called “paused” or something similar, and wrap all of your step code (except the step code on your pause controller) in a check that looks like this:
if(!paused) { //Step code goes here }
Your pause button object would then take a click and set paused to true, stopping all the step code.
Personally, I feel that that is ugly, and hard to maintain. I do not like solutions which involve adding the same boilerplate code to everything. My preference is to use the instance_deactivate_all function to handle pausing for me.
To do this, you need to set a global which we’ll call gamePaused to true:
globalvar gamePaused gamePaused = false;
Create a pause button object with a pause button sprite set to it’s default sprite. Add the following code to the step event:
if (global.gamePaused == false && mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y,pauseButtonObject)) { instance_deactivate_all(true); global.gamePaused = true; sprite_index = playButtonSprite; } else if (global.gamePaused == true && mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y,pauseButtonObject)) { instance_activate_all(); global.gamePaused = false; sprite_index = pauseButtonSprite; }
This checks to see if the game is paused. If it isn’t, and the left mouse button is pressed, and it is pressed over the pause button, deactivate everything but the pause button, set the gamePaused variable to true and change the sprite to a play button. If the game is paused, and the left mouse button is pressed, and it is pressed over the play button, reactivate everything, set paused to false and change the sprite back to the pause button sprite.
If you want to make a pause menu, create the instance in the first conditional.