informa
/
2 MIN READ
Blogs

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

Vancouver, BC, Canada
5.8.23
Producer

Bladework games

Remote (United States)
5.18.23
Senior Gameplay Engineer

University of Canterbury

Christchurch, Canterbury, New Zealand
5.17.23
Academic in Game Arts and Animation

Fred Rogers Productions

Hybrid (424 South 27th Street, Pittsburgh, PA, USA
5.19.23
Producer - Games & Websites
More Jobs   

CONNECT WITH US

Explore the
Advertise with
Follow us

Game Developer Job Board

Game Developer

@gamedevdotcom

Explore the

Game Developer Job Board

Browse open positions across the game industry or recruit new talent for your studio

Browse
Advertise with

Game Developer

Engage game professionals and drive sales using an array of Game Developer media solutions to meet your objectives.

Learn More
Follow us

@gamedevdotcom

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