How could I Set mouse position via Blueprint?

Hi there!

I want to create WoW-like mouse look system. Hide mouse, disable mouse click events and rotate character while holding right mouse button. The problem is 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.
There is one topic about that problem Link , but the solution is in c++ . Any suggestions how to solve this problem via Blueprint? (i found only “get mouse position” node)

Hey,

What type of Blueprint are you attempting this in, try doing it in a Controller Blueprint.

You could do something like this:

There is probably other ways to do it, but this should get you started with some ideas.

-W

Thanks for detailed explanation. I have tested it before in controller blueprint.

And it looks almost the same as yours:

But it still doesn’t solve the problem, maybe I’ve described situation not so good. In other words when mouse button is released I need to teleport/move mouse cursor in position where it was when mouse button was pressed. Press RMB->Rotate camera->Release RMB and got the same mouse cursor position.

I am having the exact same issue. When the right-mouse button is clicked I am saving the mouse position to a var and hiding the mouse cursor. When the right-mouse button is released I am showing the mouse cursor again and I need to move the mouse position back to the location saved in the variable. I do not see anyway to do this in the blueprint. I am going to start searching the C++ code. I’ll let you know what I find.

Dont think there is anyway in standard unreal at the moment but have a node for it in his plugin.


The Set/Get Mouse Position nodes work directly with the player's viewport, and do not require access to the HUD canvas.

~ Set Mouse Position - SET the mouse position to any values of your choosing!

~ Get Mouse Position - Get the current mouse position, will be consistent with results of SET Mouse Position

~ Get Center Of Viewport - Obtain the coordinates of the center of the viewport! Works in PIE as well as standalone game 
instances

For more info checkout the forum post about it
https://forums.unrealengine.com/showthread.php?3851-(39)--s-Extra-Blueprint-Nodes-for-You-as-a-Plugin-No-C-Required!

You can do that with standard C++ Unreal API from viewport classes, made them avable on blueprint :slight_smile:

I got it to work using a combination of Blueprints and C++ by doing the following:

I created a new C++ class called MyPlayerController to inherit from the default PlayerController class.

I added the following function to MyPlayerController using the code from your link above:

h File:

UFUNCTION(BlueprintCallable, Category = "Game|Player")
void SetMousePosition(float LocationX, float LocationY);

cpp File:

void AMyPlayerController::SetMousePosition(float LocationX, float LocationY)
{
	FViewport* v = CastChecked<ULocalPlayer>(this->Player)->ViewportClient->Viewport;
	int intX = (int)LocationX;
	int intY = (int)LocationY;
	v->SetMouse(intX, intY);
}

Then I went back to my Controller blueprint and used File->Reparent Blueprint to change the parent class to MyPlayerController. Now my new function: SetMousePosition is available in the controller blueprint and I can finish my WoW-like mouse look functionality.

I have only be using UE4 for 3 days, but I have a feeling this might be the trick to getting a lot of stuff to work. Instead of using the default classes, create your own versions in C++ and then you can expose additional functionality to the blueprints :slight_smile:

1 Like

Its good when you know c++ =).Thanks for example , such mini tutorials kinda helps me understand how c++ works.

What if I want to set mouse cursor in the center of screen when my widget popup?

This code works as of UE4 4.13

UFUNCTION(BlueprintCallable, Category = "Game|HUD", meta = (WorldContext = "WorldContextObject"))
static void SetMousePosition(const UObject* WorldContextObject, int32 X, int32 Y);


void UMyBlueprintLibrary::SetMousePosition(const UObject* WorldContextObject, int32 X, int32 Y)
{
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	APlayerController* PC = World->GetFirstPlayerController();
	if (!IsValid(PC))
	{
		return;
	}

	auto LP = Cast<ULocalPlayer>(PC->Player);
	if (!IsValid(LP))
	{
		return;
	}

	auto VP = LP->ViewportClient;
	if (!IsValid(VP))
	{
		return;
	}

	FSceneViewport* GVP = VP->GetGameViewport();
	if (GVP != nullptr)
	{
		GVP->SetMouse(X, Y);
	}
}


UFUNCTION(BlueprintCallable, Category = "Game|HUD", meta = (WorldContext = "WorldContextObject"))
static void CenterMousePosition(const UObject* WorldContextObject);

void UMyBlueprintLibrary::CenterMousePosition(const UObject* WorldContextObject)
{
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	APlayerController* PC = World->GetFirstPlayerController();
	if (!IsValid(PC))
	{
		return;
	}

	auto LP = Cast<ULocalPlayer>(PC->Player);
	if (!IsValid(LP))
	{
		return;
	}

	auto VP = LP->ViewportClient;
	if (!IsValid(VP))
	{
		return;
	}

	FSceneViewport* GVP = VP->GetGameViewport();
	if (GVP != nullptr)
	{
		const FIntPoint VPSize = GVP->GetSizeXY();
		GVP->SetMouse(VPSize.X /2, VPSize.Y / 2);
	}
}

I found a way around this by SetMousePosition to the same position it’s currently at.

This section of blueprint was run off of a Sequence after an EventTick node. Checking for player distance to the intractable object that the cursor is over. Hope this helps someone. :slight_smile:

2 Likes

image
added code from linked C++ solution in cpp file and did this in controller BP.
Dont know why but it works perfectly