Skip to main content
  1. Posts/

How To Check Viewport Is Focused

English Post Programming UnrealEngine C++ Unreal Engine

When you have multiple PIE viewports, you might have some viewport focus issues.

For example, having spectator to player and input mode change make viewports do ping pong each other.

It only happens in PIE mode because it’s not a Standalone Game.

Here’s the code on how to check my viewport is in focus.

#if UE_EDITOR
UWorld* World = GetWorld();
if (IsValid(World) && World->WorldType == EWorldType::PIE)
{
    UGameViewportClient* GameViewportClient = World->GetGameViewport();
    if (!IsValid(GameViewportClient))
        return;

    if (IsValid(GEngine) && IsValid(GEngine->GameViewport) && (GEngine->GameViewport->Viewport != nullptr))
    {
        const bool bIsFocusedViewport = GameViewportClient->IsFocused(GEngine->GameViewport->Viewport);
        
        UE_LOG(LogTemp, Log, TEXT("%s"), (bIsFocusedViewport ? TEXT("FOCUSED") : TEXT("NOT FOCUSED")));
    }
}
#endif //UE_EDITOR

Cheers!

Related

TActorIterator and TActorRange
English Post Programming UnrealEngine C++ Unreal Engine
How To Check Class Is From Native Or Blueprint
English Post Programming UnrealEngine C++ Unreal Engine Class
How To Iterate Over UENUM
English Post Programming Unreal Engine C++ Unreal Engine Enum
Controlling C++ Compile Optimization
English Post Programming C++
Quantized (양자화) 되었다는 의미와 사용하는 이유
Korean Post Unreal Engine Programming Unreal Engine Network Optimize
C++ Compile Optimization 일부분만 해제하는 방법
Korean Post Programming C++ Optimize