How to convert world coordinates to screen coordinates?

I need to get the player’s location in the world and convert that to screen coordinates so I can initially set the mouse position there.

Hello there!

You can do this by getting the SceneView (in your playercontroller for example):

ULocalPlayer* LocalPlayer = Cast(Player);

if (LocalPlayer != NULL && LocalPlayer->ViewportClient != NULL && LocalPlayer->ViewportClient->Viewport != NULL)
	{
		FSceneViewFamilyContext ViewFamily( FSceneViewFamily::ConstructionValues(
			LocalPlayer->ViewportClient->Viewport,
			GetWorld()->Scene,
			LocalPlayer->ViewportClient->EngineShowFlags )
			.SetRealtimeUpdate(true) );

		FVector ViewLocation;
		FRotator ViewRotation;
		FSceneView* SceneView = LocalPlayer->CalcSceneView( &ViewFamily, /*out*/ ViewLocation, /*out*/ ViewRotation, LocalPlayer->ViewportClient->Viewport );

		if (SceneView)
		{
             auto MyWorldPosition = FVector(YOURVALUESGOHERE);
             FVector2D MyResult;  
			    SceneView->WorldToPixel(MyWorldPosition, MyResult);
		}
	}

Thanks. I added this line at the end of the if (SceneView) {} block:

LocalPlayer->ViewportClient->Viewport->SetMouse(MyResult.X, MyResult.Y);

I suspect that is right, but I can tell my mouse cursor isn’t being set there. Any ideas why it doesn’t work? I’m calling this in the Tick function of my PlayerController class.

Figured it out. I just had to change this:

Cast(Player)

to this:

Cast(Player)

Works like a charm now. Thanks.

Edit: Well no wonder. The /< /> symbols and everything between them are automatically edited out on this site. For anyone else who comes across this it’s:
Cast</ ULocalPlayer/>(Player) w/out the slashes of course. :slight_smile: