Disable LockMouseToViewport

I tried to SetInputMode(InputModeGameAndUI) (where InputModeGameAndUI is an FInputModeGameAndUI) in my ACustomPlayerController class, but with no success (the custom player controller does work, since it enables to show my mouse cursor).

I just want to disable bLockMouseToViewport, how should I do this (and where can I find the documentation for this)?

This should work:

FInputModeGameAndUI InputMode;
InputMode.SetHideCursorDuringCapture(false);
InputMode.SetLockMouseToViewport(false);
SetInputMode(InputMode);

If it doesn’t, something else is wrong. Make sure you’re also setting bShowMouseCursor = true and CurrentMouseCursor = EMouseCursor::Default (or whatever) in your player controller.

Those warnings are unrelated.

Something else in your project must be affecting mouse locking. You’ll need to provide more information about your project. The code posted above definitely works in isolation, I just tried it.

No it does not work. I have those two warnings (I am not sure they are related):

LogActor:Warning: GameSession /Game/UEDPIE_0_Untitled.Untitled:PersistentLevel.GameSession_4 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily

LogActor:Warning: GameNetworkManager /Game/UEDPIE_0_Untitled.Untitled:PersistentLevel.GameNetworkManager_4 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily

Thx!
Here is MyPlayerController.h:

UCLASS()
class BUILDER5_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()
	AMyPlayerController(const class FPostConstructInitializeProperties& PCIP);
	FInputModeGameAndUI InputMode;
};

Here is MyPlayerController.cpp:

AMyPlayerController::AMyPlayerController(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	bShowMouseCursor = true;
	CurrentMouseCursor = EMouseCursor::Default;
	UE_LOG(LogTemp, Warning, TEXT("AMyPlayerController %d"), int32(bShowMouseCursor));
	InputMode.SetHideCursorDuringCapture(false);
	InputMode.SetLockMouseToViewport(false);
	SetInputMode(InputMode);
}

It does log the warning:

LogTemp:Warning: AMyPlayerController 1

Ok. First of all, remove this from your class declaration in the .h:

FInputModeGameAndUI InputMode;

It doesn’t need to be stored as a member of your class.

Change your initialization function into this:

AMyPlayerController::AMyPlayerController(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	bShowMouseCursor = true;
	CurrentMouseCursor = EMouseCursor::Default;
	UE_LOG(LogTemp, Warning, TEXT("AMyPlayerController %d"), int32(bShowMouseCursor));
}

Setting the input mode in the default constructor will not have any effect on the game, so we remove it from here.

Add this to your player controller’s class in the .h:

virtual void SetupInputComponent() override;

And add this in the .cpp:

void AMyPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	FInputModeGameAndUI InputMode;
	InputMode.SetHideCursorDuringCapture(false);
	InputMode.SetLockMouseToViewport(false);
	SetInputMode(InputMode);
}

Let me know if that works for you.

Thx! Well it does not work for me… I can log something in SetupInputComponent() so the function is called, but it seems to have no effect.

Just to make sure this is correct: I use my AMyPlayerController class by setting PlayerControllerClass = AMyPlayerController::StaticClass(); in my custom game mode class (which is selected in my world settings, and used), in the constructor.

Side question: how am I suppose to know that I should use functions like SetupInputComponent, by looking at the PlayerController class or is it documented somewhere? (How did you know?)

Thx! This is the most important part of MyPawn.cpp, the rest is to handle inputs and set the camera accordingly:

// In the constructor:
AutoPossessPlayer = EAutoReceiveInput::Player0;

	// Create a dummy root component we can attach things to.
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	// Create a camera and a visible object
	// UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
	OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
	// Attach our camera and visible object to our root component. Offset and rotate the camera.
	//OurCamera->AttachTo(RootComponent);
	OurVisibleComponent->AttachTo(RootComponent);

	//Create our components
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	OurCameraSpringArm->AttachTo(RootComponent);

	OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
	OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);

Yes, that’s correct.

Ok, something else must be causing the input mode to change. It might be your pawn class. Can you post it?

I learned by looking at the example projects and the new Unreal Tournament.

Ok, let’s try a couple of things and see if it has any effect. In your MyPawn constructor in MyPawn.cpp, add this:

bAddDefaultMovementBindings = false;

And then in your MyPlayerController.h, add this:

virtual void Tick(float DeltaTime) override;

And then in MyPlayerController.cpp, add this:

void AMyPlayerController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	FInputModeGameAndUI InputMode;
	InputMode.SetHideCursorDuringCapture(false);
	InputMode.SetLockMouseToViewport(false);
	SetInputMode(InputMode);
}

And tell me if that works.

Edit:

Sorry, I forgot to say you should add:

PrimaryActorTick.bCanEverTick = true;

To your MyPlayerController constructor

Is bAddDefaultMovementBindings a property of APawn?
I can’t find it here, nor Visual Studio (using UE4.8.3).

When I SetInputMode(InputMode); in the Tick event, the camera does not move anymore.

My mistake, it’s only a property on certain types of pawns (ADefaultPawn). Remove that part and ignore it.

No, you are right, it is not locked! So it means my settings are overridden somewhere, maybe when I do AutoPossessPlayer = EAutoReceiveInput::Player0;?

But is the cursor locked to the viewport?

It could be. If you want to camera to keep moving even if the mouse is not captured (locked), I think you will need to handle passing the movement input into the controller/pawns yourself. Rotation control only happens automatically if the mouse is captured.

When I click, the line 397 of SceneViewport.cpp:

 bool bShouldShowMouseCursor = World->GetFirstPlayerController()->ShouldShowMouseCursor()

causes a call to LockMouseToWidget() in Reply.h (which seems to disable the call to ReleaseMouseLock() performed by SetInputMode() )

I do have bindings to the input events in my AMyPawn class to control the pawn and camera moves (rotation and translation).