Execute Blueprint Functions when Application gets focus/loses focus

Hey, the problem is as follows:

My VR Application starts an external VR application which is then in focus. Ending the Application brings the Unreal Application again in focus. The problem is, tho the application is running fine, the image freezes. This problem can be solved by disabling the HMD and enabling it again, for example when the key “R” is pressed. How can I automatically disable/enable the HMD when the application gets the focus back?

Regards

I think you’ll have to detect it in C++ and call out to your blueprint function.

I already saw that thread but its not exactly what i want to do. I need an event that detects only once if the focus is lost or the focus got back. It goes more in the direction of UGameViewportClient::LostFocus and UGameViewportClient::ReceivedFocus but Im not sure how to implement it. I`m relatively new to c++ and unreal programming. I already looked at https://answers.unrealengine.com/questions/225658/application-lost-focus-event.html

Hi phil070,

As you mention it, you should use UGameViewportClient::Lost/ReceiveFocus.

You have to create a class that derive from UGameViewportClient

Put this in the .h file :

#pragma once
 #include "Engine/GameViewportClient.h"
 #include "MyGameViewportClient.generated.h"
 
 /**
  * UMyGameViewportClient class.
  */
 UCLASS(Blueprintable)
 class GAME_API UMyGameViewportClient : public UGameViewportClient
 {
     GENERATED_BODY()
 
 public:
     // Override lost focus function | This function will call your blueprint function.
     virtual void LostFocus(FViewport* Viewport) override;

     // Override lost focus function | This function will call your blueprint function.
     virtual void ReceiveFocus(FViewport* Viewport) override;

     UFUNCTION(BlueprintImplementableEvent, Category="LostFocus")
     public void ActionToDoBeforeLostFocus();

     UFUNCTION(BlueprintImplementableEvent, Category="ReceiveFocus")
     public void ActionToDoAfterReceiveFocus();
 };

put this in de .cpp file:

#include "Game.h"
 #include "MyGameViewportClient.h"
 
 void UMyGameViewportClient::LostFocus(FViewport* Viewport)
 {
     UMyGameViewportClient::ActionToDoBeforeLostFocus(); //call our blueprint function before that the game engine execute his own LostFocus code.

     UGameViewportClient::LostFocus(Viewport); //call the parent method
 }
 void UMyGameViewportClient::ReceiveFocus(FViewport* Viewport)
 {         
     UGameViewportClient::ReceiveFocus(Viewport); //call the parent method

     UMyGameViewportClient::ActionToDoAfterReceiveFocus(); //call our blueprint function after that the game engine execute his own ReceiveFocus code.
 }

Now you juste to define a blueprint derive this class (UMyGameViewportClient) and override the ActionToDoBeforeLostFocus and ActionToDoAfterReceiveFocus in blueprint, thats it! enjoy!
This function has no parameters, to acces any variable of the game use GameMode or GameInstance object

Let me know if it resolve your problem :wink:

Best regards,
XuruDragon

Hi XuruDragon ,

thanks for your answer. I implemented the code as you said. Then created a new Blueprint derived from MyGameViewportClient. There I have the event Action To Do Before Lost Focus but the event is still not working.

104765-lostfocus.png

Hi phil070,
Maybe you should implement the event Action to do Before Lost Focus as a function by override it. (You should have an option in the blueprint editor)
I think the events are governed by a system of dispatchers and I think the event is not just called at all. Moreover, in the C ++ code we have write the function to override as function. Yes I know this is confusing . I would try this evening after returning from work , to do a test on my platform if you stil blocked

Yes, I have two functions available to override (See Screenshot). If you could test it that would be awesome :slight_smile: PS: you got a small type error in your code. It`s ReceivedFocus not ReceiveFocus. Thanks for your help!

104766-override.png

Yep, I will test that tonigth around 10pm hour of paris beacause i’m a french guy. I will notify you after that.
Thank you, it’s write from my memory , obviously I should have checked … ^^

Yes that’s right , you can override the function that we see on the screenshot , it should do the trick :wink:

i’m sorry but I’m blocked with my Visual Studio/Unreal Engine each time I want to open Visual STudio it crash…

By the way, my solution is not suitable, because we only can override event and they can not be fired up…

Sorry phil0770 i can’t help anymore… :frowning:
You have to do it all by c++ way… :confused: