When I call FViewport::LockMouseToViewport nothings seams to happen. Is there a misunderstanding of how this function should be used or is it just not working?
I get the FViewport from casting the Player member in the PlayerController to a ULocalPlayer. Then I call localPlayer->ViewportClient->Viewport->LockMouseToViewport(true). I have tried to call it in all different places but can’t get any result.
The code seems to toggle the mouse visibility and whether look input should be ignored or not. I want to lock the mouse cursor to the window. Is it some kind of a hack? I think LockMouseToViewport would be a nicer way of achieving this behavior and probably more reliable when the engine is being updated.
I am. What I suggested in this thread isn’t a complete solution, and when I toggle from / to my program, now I’m really losing the boundaries for my mouse and it goes everywhere. I’d love to know how this function works.
Just to make sure I completely understand what you are looking for, you are trying to find a way to keep the mouse cursor from moving outside of the game window?
I am still doing some research regarding uses for LockMouseToViewport. One thing that was pointed out to me was that the Strategy Game example project keeps the mouse locked within the game window. However, it seems to accomplish this without using LockMouseToViewport. I believe this is where that logic is being implemented, but I am not 100% sure.
// SStrategySlateHUDWidget.cpp
FCursorReply SStrategySlateHUDWidget::OnCursorQuery( const FGeometry& MyGeometry, const FPointerEvent& CursorEvent ) const
{
//make sure if our application is active, we do have mouse captured in window bounds
if (FSlateApplication::Get().GetActiveTopLevelWindow().IsValid() && FSlateApplication::Get().GetMouseCaptor() != SharedThis(this))
{
FSlateApplication::Get().SetFocusToGameViewport();
}
return FCursorReply::Cursor(EMouseCursor::Default);
}
I’m not familiar with Slate, I’m using AHUD and Canvas for my 2D drawings. So can I implement a HUD in both Slate and AHUD / Canvas together ? I’ve tried Strategy Game and the mouse is limited to the window, before and after toggling back into the program. The program also mutes the sound when I toggle to another app (dunno if that’s UE4 related or Windows related) but it doesn’t pause the game - unless I pause the game explicitly with the menu button.
In a related issue, I also notice that AHUD::OnLostFocusPause() never pauses a game. And even overridden in my own child class, it never goes through it when I loose my game’s focus. That’s because it is called exclusively by UEngine::OnLostFocusPause(), line 5123, but the latter is never called by anything else. Unless I must call that function myself (doubt it) , I would need another method to receive the lost focus signal.
Thank you for your report. We were not able to investigate this on the engine version you reported, but there have been many version changes to UE4 since this question was first posted. With a new version of the Engine comes new fixes and it is possible that this issue has changed or may no longer occur. Due to timetable of when this issue was first posted, we are marking this post as resolved for tracking purposes.
If you are still experiencing the issue you reported in the current engine version, then please respond to this message with additional information and we will investigate as soon as possible. If you are experiencing a similar, but different issue at this time, please submit a new report for it.
It works to some extent. However there are still some problems. It doesn’t work at all in a standalone window. In “normal” window it works but the mouse isn’t locked to the viewport but the the window. Has it been tested at all?
I guess I will have to use slate in order to lock the mouse to the viewport, a bit awkward but what should you do.
Looking at the declaration of the virtual void function “LockMouseToViewport(bool bLock)” in the UnrealClient.h file, there is no code to implement the function. As a virtual function it would be possible to override the function elsewhere with your own code functionality.
However this doesn’t seem to work very good. Especially not when working with standalone window or running without the editor. Anyhow if there is any other way to simply lock the mouse to the viewport without having to lock it to a specific slate widget that would work too.
In 4.6 and forward there are functions that help you setup input modes.
One thing they allow is a way to control whether or not the mouse is locked, they also do quite a bit more.
In blueprints use the SetInputMode_UIOnly, SetInputMode_GameAndUI, SetInputMode_GameOnly nodes to setup your desired behavior.
Game Only always locks.
In C++ you can call the:
APlayerController::SetInputMode()
function takes FInputModeUIOnly, FInputModeGameAndUI, FInputModeGameOnly, or your own custom class.
I have actually tested those but wasn’t completely satisfied with how they worked. E.g. when I used FInputModeGameAndUI, if the window lost focus the mouse wasn’t recaptured when selecting the window again. I want the mouse to be visible all the time but never allow it to leave the window as long as the window is in the foreground.
I guess I could use the code to implementing my own input states. The problem with that would be when the engine gets updated there is a risk my code would break over time. Unreal engine has had a tendency of breaking old code when updated.