Sponsored By

UE4Cookery CPP011: Simple Pause/UnPause implementation

Topic: Simple Pause implementation

Source for UE4.27: https://github.com/klauth86/UE4Cookery/tree/main/CPP011_A

Artur Kh, Blogger

June 1, 2022

2 Min Read

In many games developers implement Pause/UnPause logic to allow players to have a short break or just to block everything in game while player is walking through UI things. Here we'll be using very simple and short approach.

First of all, engine has two different types of time:

  • FSlateApplication time - is the time from Operation System and it measures real time (our World time). It is used mainly for real time calculations, stats and UI animation.

  • UWorld time - is the time from game World and it is simulated by engine itself (game World time). All actors and objects of scene are living according to that one.

So, when we talk about Pause/UnPause logic, pausing is applied to UWorld time. With all of that in mind, first step of implementing Pause/UnPause mechanism will be to create custom UPawn class, add appropriate Input Action Mapping to it and bind this action. It will something like this:



#pragma once

#include "GameFramework/DefaultPawn.h"
#include "MyDefaultPawn.generated.h"

class CPP011_A_API AMyDefaultPawn : public ADefaultPawn


    virtual void SetupPlayerInputComponent(UInputComponent* InInputComponent) override;

    void OnPause();



#include "MyDefaultPawn.h"
#include "GameFramework/PlayerInput.h"
#include "Kismet/GameplayStatics.h"

const FName InputAction_Pause = FName("Pause");

void AMyDefaultPawn::SetupPlayerInputComponent(UInputComponent* InInputComponent)

    UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping(InputAction_Pause, EKeys::P));

    InInputComponent->BindAction(InputAction_Pause, EInputEvent::IE_Pressed, this, &AMyDefaultPawn::OnPause);

void AMyDefaultPawn::OnPause()
    bool isNowPaused = !UGameplayStatics::IsGamePaused(this);

    UGameplayStatics::SetGamePaused(this, isNowPaused);

    UE_LOG(LogTemp, Warning, TEXT("%s"), *FString(isNowPaused ? "GAME IS PAUSED!" : "GAME IS OK!"));


Everything looks fine. However, when we will try this no any Input will be processed by our handler after we paused UWorld time. This is happening because pawns are actors and they are living according to UWorld time. When it is paused, same thing occurs with all actors, their parts and components. Thus, our pawn will not consume any Input at this state.

Well, engine has straightforward solution for this case. We only need to replace this line of code


    InInputComponent->BindAction(InputAction_Pause, EInputEvent::IE_Pressed, this, &AMyDefaultPawn::OnPause);


by this one:


    InInputComponent->BindAction(InputAction_Pause, EInputEvent::IE_Pressed, this, &AMyDefaultPawn::OnPause).bExecuteWhenPaused = true;


and now everything works as we want.

Read more about:


About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like