Skip to content

Instantly share code, notes, and snippets.

@DanBradbury
Created August 18, 2014 04:25
Show Gist options
  • Save DanBradbury/f3524e3778284fe8a1df to your computer and use it in GitHub Desktop.
Save DanBradbury/f3524e3778284fe8a1df to your computer and use it in GitHub Desktop.
main

In today's episode we are going to create a pause effect that can be easily implemented into any game you may be working on. For this example we are going to build off of a minimalistic side scroller with only a few active objects.

Source code hosted on Github (pull, fork, or whatever you would like).

To implement the effect we will have o_player (this would be any control object in your game), trigger an event that creates o_pause which will handle all the logic for the effect.

o_player is the easiest part to implement so we should just take care of it first.

if(keyboard_check_pressed(ord('P'))){
    instance_create(x,y,o_pause);
}

By having the player initialize o_pause we have created a perfect Object Oriented separation which will make our code easier to understand and debug in the future. (Future episode will focus on just this topic)

Now let's walkthrough the meat of the code that creates the pause effect.

o_pause

The goal is to freeze all the items and store the details that we need to redraw them once they are deactivated. We will use an Array to hold each of the objects' information and push them onto a Stack that we will use in our Draw event.

///deactivate items
objects = ds_stack_create();

//single object
player = array(o_player.sprite_index,  o_player.image_index, o_player.x, o_player.y, o_player.image_xscale, o_player.image_yscale);
ds_stack_push(objects, player);
instance_deactivate_object(o_player);

//multiple objects
with(o_bird){
    ds_stack_push(o_pause.objects, array(sprite_index, image_index,x,y, image_xscale, image_yscale));
}
instance_deactivate_object(o_bird);

The first thing that will stand out is the use of the array() function. This is a custom script that I have written to create an array in a convenient way. IMO GameMaker fails to provide helpful initializers for arrays and other data structures that I use on a regular basis.

The other thing of interest is the use of the with construct, which we can use to iterate on all existing objects with the given name. When we are inside the block we have direct access to the instance of the object and because of this you may have noticed the use of o_pause.objects even though we are technically writing code within that object.

Since we have all our objects in a Stack we will use ds_stack_empty to iterate on each of the values. Ideally we would be able to use a List or another data structure but each seems to have issues storing Arrays (check out the how limiting Lists are).

temp = ds_stack_create();
ds_stack_copy(temp, objects);

while(!ds_stack_empty(objects)){
    obj = ds_stack_pop(objects);
    draw_sprite_ext(obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], 0, c_white, 1);
}

objects = temp;

Since Stacks have there own unique behavior ( Last In First Out [LIFO] ), and accessing items requires removing them from the structure we need to handle those caveats. To do this we use another temporary Stack to hold a clone and ensure we restore the state for the next iteration.

The final step is to destroy the effect and resume the game.

if(keyboard_check_pressed(vk_enter)) {
    instance_activate_all();
    instance_destroy();
}

And just like that we have created a successful base Pause effect that we can build off of.

For this example I decided to finish the effect with an extra draw event that draws the transparent layer and "Game Paused" text.

draw_set_alpha(0.75);
draw_set_color(c_black);
draw_rectangle(0,0,room_width, room_height,false);
draw_set_alpha(1);
draw_set_color(c_white);
draw_text((view_xview[0]+view_wview[0])/2,view_yview[0]+view_hview[0]/2,"GAME PAUSED");

Be sure to set the depth to be in front of all objects that are visible (possibly leave HUD above)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment