Has anyone got LockMouseToViewport() to work ?

I’ve been struggling to keep my mouse cursor inside my game, and with a dual monitor my mouse just goes as far as my desktop can reach. I guess for a single screen desktop running a fullscreen application, this is not a critical issue, but it’s still not “locked” in the window. So I tried this method


GEngine->GameViewport->Viewport->LockMouseToViewport(true);

inside my MyHUD::PostInitializeComponents(), inside my characters components, wherever.

So, is this the correct way to use this function (I got this from an AnswerHub answer from ShadowRiver).
Is there a specific location where I should use it ?

I don’t know if it should be working for 4.2, but it’s certainly not working for 4.1.

That is because it has no code in it - this is the declaration:



virtual void LockMouseToViewport( bool bLock )	{ }


I knew that. :slight_smile: But that’s the default viewport definition. I use a Third-Person template and the viewport is derived from FSceneViewport - at least that’s where I landed when I traced my code into the Engine - and that has code in it.

I got it to work on a very specific case but it’s not easy to reproduce and not very coherent. Something about the first click touching a target I defined with an OnClick() in Blueprint. Then the view port locks the mouse in the Editor’s viewport.

But in retrospect, that’s not what I’m looking for. FViewport/FSceneViewport seem to be reserved for the Editor viewport only. I’m looking for a way to limit my mouse into windowed games.

Thanks for the answer. But if you have anything else on my problem feel free to ring me. :slight_smile:

Edit:

Well I got it to work into a windowed game after all. I think the basic problem is how my game handles mouse “capture” into the game. You know, when the Editor says “Click for Mouse Control”, then “Press Shift-F1 for Mouse Cursor”, well in my case it toggles back to it immediately to the first case. And it’s only when the game really keeps that mouse in control that it locks the viewport. The culprit is probably in my code :frowning: I’ll need to investigate that.

Peace.

So how have you achieved to lock the mouse? I’m interesed in doing that to so I’ll be happy for a code sample. :smiley:

Thanks in advance.

I’m working the issue. So far, I believe that LockMouseToViewport() has nothing to do with it. I removed it and I have the same erratic behaviour. So there goes one theory. And any code I wrote seems to break things, not fix them. :slight_smile:

Keep watching this thread.

I have no idea why my first experiment with it failed and that is why I thought LockMouseToViewport() would help in some way: it doesn’t. But if this helps anybody else, my post will not have been made in vain. :slight_smile:


This is a simplified version of Rama’s HUD tutorial to a bare minimum where the only thing happening here is a toggle between character control and mouse cursor display. I used it on a Third-Person template. I haven’t tried it on other templates. Beyond that, YMMV.

Original tutorial: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums,Create_Buttons%26_Draw_Materials

The mouse should be locked into the window when “F” is pressed. Just like the tutorial says, don’t forget to create your Blueprint HUD and include HUD creation code into your Game Mode constructor.

/ht Rama. I love your code, but sometimes we need to break it down a little just to keep things under control. :slight_smile:

MyHUD.h



#pragma once

#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"

/**
 * 
 */
UCLASS()
class AMyHUD : public AHUD
{
	GENERATED_UCLASS_BODY()

protected:

	void PostInitializeComponents();
	void PlayInputChecks();
	virtual void DrawHUD() OVERRIDE;

public:

	APlayerController* ThePC;
};


MyHUD.cpp



#include "MuGame.h"
#include "MyHUD.h"

AMyHUD::AMyHUD(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}

void AMyHUD::PostInitializeComponents()
{
	Super::PostInitializeComponents();
 
	//Establish the PC
	ThePC = GetOwningPlayerController();
}

void AMyHUD::DrawHUD()
{
	//Have PC for Input Checks and Mouse Cursor?
	if(!ThePC)
	{
		//Attempt to Reacquire PC
		ThePC = GetOwningPlayerController();
 
		//Could Not Obtain PC
		if(!ThePC) return;
		//~~
	}
 
	//Multiplayer Safety Check
	if(!ThePC->PlayerInput) return; //not valid for first seconds of a multiplayer client

	PlayInputChecks();
}

void AMyHUD::PlayInputChecks()
{
	if(ThePC->WasInputKeyJustPressed(EKeys::F))
	{
		bool  lookinput = !ThePC->IsLookInputIgnored();

		ThePC->SetIgnoreLookInput(lookinput);
		ThePC->bShowMouseCursor = lookinput;

		return;
	}
}