My game locks the cursor in the center of the screen when the character is being controlled.
Is there a way to check if the player has tabbed out of the game window?
My game locks the cursor in the center of the screen when the character is being controlled.
Is there a way to check if the player has tabbed out of the game window?
Forgot to put this in the question, but the cursor continues to lock in place when the window isn’t focused.
Pause Game When Window Loses Focus:
https://benui.ca/unreal/window-focus-change/
Thank you. I looked around quite a bit and couldn’t find a way to do it with blueprint. Gonna learn how to plug this into my game now.
Could you explain in what situations you want to use it? Is it for PlayerController?
I mainly use C++, I can help you.
There’s no node to check that, you can do a workaround using:
Get Game User Settings → Get Frame Pace
Normally (but you can change that) when you have the Game Window unfocused it will drop FPS significantly and you can use that fact to determine wether the game window is active or not
For convenience add a delay to re-check if it’s still a low value to prevent code from running if you got a low fps value for a short moment due to performance hit
I’m just using a Character class.
I would like to get a boolean that changes value when the window is focused or unfocused and be able to check its value from my Character in Blueprint.
I have it set to repeatedly set the cursor position to the center of the screen if the game is not paused. Just going to add the new boolean to the “if” node.
You can inherit a Blueprint Character from your Character C++ class:
.h file:
UCLASS()
class EXAMPLE_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
public:
UFUNCTION(BlueprintImplementableEvent)
void OnWindowsLostFocus();
UFUNCTION(BlueprintImplementableEvent)
void OnWindowsGainFocus();
private:
void OnWindowFocusChanged(bool bIsFocused);
};
.cpp file:
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
FSlateApplication::Get().OnApplicationActivationStateChanged().AddUObject(this, &AMyCharacter::OnWindowFocusChanged);
}
void AMyCharacter::OnWindowFocusChanged(const bool bIsFocused)
{
if (bIsFocused)
{
OnWindowsGainFocus();
}
else
{
OnWindowsLostFocus();
}
}
And use:
Thank you! I’ve got it working in my project now, also learned a few things along the way.
Thanks, I solved the problem with other reply but is there an optimal fps number to check for or is it different for each project?
That would be incredibly unreliable. I believe there’s a INI setting that sets the game to run at full speed or not when not focused, which a user could change too. Better to use the OS events.
Yeah, regarding reliability - you’re totally right, such a workaround will not give you an almost 100% probability… far from that
Unfortunately there wasn’t anything better I could come up with
You can maintain the FPS Features in Settings, but for now I am at work and cannot tell if this is Project specific or Engine Settings
Victory plugin has a node for this I believe. like game window is foreground in os or something.
I have a feeling Rama thought of everything… a patron saint of blueprinters
PS.: Since some people still love to go around accusing others of such a thing, I have to point out that according to google this was the most relevant discussion about the topic so this is not necro posting.
This does not work in version 5.4.4. Can someone suggest what is the reason? My project doesn’t start after compilation in MVC.
Could you post the error log?
If anyone ever will have the same problem and doesn’t know/want to use C++, I made a simple plugin exposing just that event to the blueprints.