YoYo Games Wiki

Pausing

From YoYoGames Wiki

Some or all of the functions in this article may work only in the registered version of Game Maker.

Game Maker does not have an internal function for pausing, which can complicate matters if you want to add a pause button to your game. While the sleep() function will pause your game for a set amount of milliseconds, this is not very tactical, because the pause can not be cut-off early. So for those of you who want to pause you Game Maker games, here are some ways to do it.

Method 1: Using keyboard/mouse_wait()

The first method you can use to pause your game is the two functions keyboard_wait(key) or mouse_wait(key). These functions simply pause the game until the given key (or button) is pressed, at which point the game resumes. The code should be structured something like this:

//Add draw events here to draw something (like a pause box), before pausing.
screen_refresh();  //redraws the screen, making your box visible
keyboard_wait(vk_all); //use a different key index to change what key is pressed.
io_clear(); // clears keyboard\mouse states, so the key you pressed does not toggle events.

This method is somewhat simple, but it will completely freeze the game, so nothing can be animated, nor will particles be able to move.

Method 2: Using while expression

The second method to pausing your game is to do something causes the computer to become stuck in a loop until a key is pressed. You can do such a thing with a while loop. Be careful though, as this method is very touchy, and incorrect programing can cause the game to become stuck in an 'infinite loop' (if this happens, use Ctrl+Alt+Del to open Task Manager and shut down the game). The code would be formatted something like this:

//Add draw events here to draw something (like a pause box), before pausing (optional)
screen_refresh();  //redraws the screen, making your box visible (optional if used below)
while (1) //loops indefinitaly
{
   //add other code here (optional)
   if keyboard_check_direct(<some key>)
   then exit;
   sleep(30); //this causes the loop to 'slow down'
}
io_clear(); //clears keyboard\mouse states, so the key you pressed does not toggle events.

This method is more 'customizable' than the previous method, as you can 'animate' a sprite by using a variable in the while event, like this:

//start this variable outside the while event
var img_ind;
//this goes inside the while event, before a screen refresh
draw_sprite(sprite,img_ind,x,y);
screen_refresh();
img_ind+=1;
if (img_ind==image_number_of_sprite)
then img_ind=0;

However, unless you create a background or sprite resource from the screen before pausing and draw that resource on the screen at the beginning of each loop, any previous drawing done on the screen will remain there, causing a 'trailing' effect.

You can also update particle systems using the part_system_update(ind) function, and then refreshing the screen. Note that you can use this function even without capturing and drawing the screen (as explained above), because each particle system has an internal surface which it clears each time it updates. It then draws this surface on the screen itself (not the internal image).

Method 3: Creating a background from screen

This third method is the most versatile method there is for pausing, as it not only pauses the game, but allows it to continue at the same time, and can support moving particles and animations. However, it does require the Registered version of Game Maker. The basics of this method is that you have a object specifically for pausing (NOT the player), having one create event,an event for the pause key, a key to unpause, and a draw event with a variable toggle (this toggle variable can be initiated in a create event). Create Event:

  • Create the toggle variable that will tell the object to draw the 'paused' background.

Pause key Event:

  • Put code that captures the screen and saves it to a background (or saves over a previous background made for pausing)
  • Then deactivate all other objects except itself
  • Then 'turn on' the toggle variable.

Un-pause key Event:

  • Deactivate the toggle variable.
  • Activate all instances.

Draw Event:

  • Check if toggle variable is 'on'.
  • If so, draw the 'paused' background, and maybe a paused box.

Using this method, you can still create particles, and do a lot of other stuff, because the room is still running at 30 fps.