More of a text snippet than a tutorial, this is how it's possible to call a function when the game loses focus.

In this example we have put the callback function in a custom APlayerController subclass, but it could be just about anywhere.

Also it might be worth wrapping this feature in a UUserGameSetting property so players can disable it if they don't want it.

First we need to add Slate to our MyProject.Build.cs file:


public class MyProject : ModuleRules
	public MyProject(ReadOnlyTargetRules Target) : base(Target)
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
		PublicDependencyModuleNames.AddRange(new string[] {
			"Slate" // Add this!

		PrivateDependencyModuleNames.AddRange(new string[] {  });

Next, we need to add to our player controller in both the .h and the .cpp:


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "BUIPlayerController.generated.h"

class ABUIPlayerController : public APlayerController
	virtual void BeginPlay() override;
	void OnWindowFocusChanged( bool bIsFocused );


#include "BUIPlayerController.h"
#include "Framework/Application/SlateApplication.h"

void ABUIPlayerController::BeginPlay()
		.AddUObject( this, &ABUIPlayerController::OnWindowFocusChanged );

void ABUIPlayerController::OnWindowFocusChanged( bool bIsFocused )
// Don't pause in the editor, it's annoying
	if ( bIsFocused )
		// Unlimit game FPS
		GEngine->SetMaxFPS( 0 );

		// Unpause the game
		// MyHUD->SetPause( false );
		// Reduce FPS to max 10 while in the background
		GEngine->SetMaxFPS( 10.0f );

		// Pause the game, using your "show pause menu" function
		// MyHUD->SetPause( true );