How do I set the mouse position in a PlayerController?

I am trying to recreate WoW like camera controls. When you press the right mouse button , the mouse disappears and you can rotate the camera.

My code looks like this


void AMyProject3Character::RightMousePressed(){
	APlayerController* pc = GetPlayerController();
	pc->bShowMouseCursor = false;
	pc->SetIgnoreLookInput(false);
	pc->GetMousePosition(LastMousePos.x,LastMousePos.y);

}
void AMyProject3Character::RightMouseReleased(){
	APlayerController* pc = GetPlayerController();
	pc->bShowMouseCursor = true;
	pc->SetIgnoreLookInput(true);
	//SetMouseCursor?
}

Which kinda works but the problem is even if I disable the mouse cursor it still move around. So when you press the right mouse button, the mouse disappears and you can rotate the camera. But when you release the right mouse button the mouse appears at a different location.

So I thought maybe I could safe the mouse position and then set it again.

The problem is there is no setter for the mouse position in the playercontroller.

I’ve set up the Wow like camera controls through BP’s but I need to reset the mouse coordinates once the player has finished rotating the camera.

So can I just bump this thread and ask how to set Mouse Coords in blue prints as there is a ‘Get Mouse Position’ function, but no ‘Set Mouse Position’

EDIT: Sorry just realised this in the C++ rather than Blue Print section.

Oh sorry I forgot to post the answer


FViewport* v = CastChecked<ULocalPlayer>(pc->Player)->ViewportClient->Viewport;
v->SetMouse(LastMousePos.x, LastMousePos.y);

2 Likes

Thanks maikklein. Just wondering, but did you do this in C++ because it can’t be done in BluePrints?

I don’t use blueprints but I am fairly certain that you can do it in blueprint.

This is exactly what I want, but how do I do this in Blueprint?

i would also like to know how to do this in blue prints. =D

I am using google for last 2 hours trying to solve how to do this in blueprint. =)

Looks that for some reason there is no blueprint functionality atm, and we have to use C++ implementation. I am still figguring out how to connect C++ with the blueprints, but looks I’ll solve it soon.

using setmouseposition() in practice you could use a gamepads joystick to move the mouse cursor I’m assuming in C++, but is there a function that I could call to simulate a mouse button being pressed or is there a command in UMG that I can call if button A is pressed and the mouse pointer is over a button then press it?