I want to do things when a player alt tab’s the game or something else causes him to lose focus from the whole game and onto another application.
I heard about using UGameViewportClient::LostFocus, but it doesn’t work correctly. When someone clicks on a UMG widget UGameViewportClient::LostFocus is called, which is very much not the intended behaviour. Is this a bug? If not how do I actually tell if the entire application has lost focus?
You can call IsForegroundWindow() on your LocalPlayer’s ViewportClient->Viewport.
Something like:
ULocalPlayer* LocPlayer = Cast<ULocalPlayer>(Player);
if (!LocPlayer->ViewportClient->Viewport || !LocPlayer->ViewportClient->Viewport->IsForegroundWindow())
{
// viewport is either not present or not in the foreground.
}
…should work for you. Player lives within the PlayerController, so the above code will work if called from within a playerController, or can be called somewhere else by grabbing the Player from the local playerController.