Search code examples
game-engine2d-gamesgmlgame-maker-studio-2

How can I add text like "Game is paused" when I pause the game in GameMakerStudio2


I have a code to when I press "p" the game pauses. Although, I want to show some text saying like "Game is Paused. Press P to progress" how can I do that? Here´s my code:

//create event
pause=false;
pauseSurf=-1;
pauseSurfBuffer=-1;

resW=1920;
resH=1080;

//Post-Draw event
gpu_set_blendenable(false);

if(pause)
{
    surface_set_target(application_surface);
        if(surface_exists(pauseSurf)) draw_surface(pauseSurf,0,0);
        else // restore from buffer if we lost the surface
        {
            pauseSurf = surface_create(resW,resH);
            buffer_set_surface(pauseSurfBuffer,pauseSurf,0);
        }
  
    surface_reset_target();
}

if(keyboard_check_pressed(ord("P")))// Toggle pause(Whatever condition/trigger you like)
{
    if(!pause)// pause now
    {
         pause=true;
        // deactivate everything other than this instance
         instance_deactivate_all(true);
        // NOTE:
        // If you need to pause anything like animating sprites,tiles,room backgrounds 
        // you need to do that separately,unfortunately!
        // capture this game moment(won't capture draw gui contents though)
         pauseSurf=surface_create(resW,resH);
         surface_set_target(pauseSurf);
             draw_surface(application_surface,0,0);
         surface_reset_target();
        // Back up this surface toabuffer in case we lose it(screen focus,etc)
        if(buffer_exists(pauseSurfBuffer)) buffer_delete(pauseSurfBuffer);
        pauseSurfBuffer=buffer_create(resW*resH*4,buffer_fixed,1);
        buffer_get_surface(pauseSurfBuffer,pauseSurf,0);
    }
    else // unpause now
    {
         pause=false;
         instance_activate_all();
         if(surface_exists(pauseSurf))surface_free(pauseSurf);
         if(buffer_exists(pauseSurfBuffer))buffer_delete(pauseSurfBuffer);
    }

}

gpu_set_blendenable(true);

//Clean up event
if(surface_exists(pauseSurf))surface_free(pauseSurf);
if(buffer_exists(pauseSurfBuffer))buffer_delete(pauseSurfBuffer);

Code from: https://www.youtube.com/watch?v=dNiLIX8jNOM&t=95s&ab_channel=ShaunSpalding

If any of you knows how to help me I would be thankful! :)


Solution

  • Add a DrawGui Event to your object, and then add the following code within:

    if (pause)
    {
        draw_text(50, 50, "Game is Paused. Press P to progress");
    }
    

    DrawGui makes it so that it renders on top of your viewport, so it's not connected with the position in the room.

    The 50, 50, is the X and Y position of the text, use it as you see fit. You can use it centered if you take the width/height of the camera/viewport and divide that by 2.

    The pause is already defined in the Create Event, so that shouldn't give any problems.