informa
2 min read
article

UE4Cookery CPP011: Simple Pause/UnPause implementation

Topic: Simple Pause implementation Source for UE4.27: https://github.com/klauth86/UE4Cookery/tree/main/CPP011_A

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:

 

MyDefaultPawn.h

#pragma once

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

UCLASS()
class CPP011_A_API AMyDefaultPawn : public ADefaultPawn
{
    GENERATED_BODY()    

protected:

    virtual void SetupPlayerInputComponent(UInputComponent* InInputComponent) override;

    void OnPause();
};

 

MyDefaultPawn.cpp

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

const FName InputAction_Pause = FName("Pause");

void AMyDefaultPawn::SetupPlayerInputComponent(UInputComponent* InInputComponent)
{
    Super::SetupPlayerInputComponent(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.

Latest Jobs

Treyarch

Playa Vista, California
6.20.22
Audio Engineer

Digital Extremes

London, Ontario, Canada
6.20.22
Communications Director

High Moon Studios

Carlsbad, California
6.20.22
Senior Producer

Build a Rocket Boy Games

Edinburgh, Scotland
6.20.22
Lead UI Programmer
More Jobs   

CONNECT WITH US

Register for a
Subscribe to
Follow us

Game Developer Account

Game Developer Newsletter

@gamedevdotcom

Register for a

Game Developer Account

Gain full access to resources (events, white paper, webinars, reports, etc)
Single sign-on to all Informa products

Register
Subscribe to

Game Developer Newsletter

Get daily Game Developer top stories every morning straight into your inbox

Subscribe
Follow us

@gamedevdotcom

Follow us @gamedevdotcom to stay up-to-date with the latest news & insider information about events & more