When I use the following code in a custom AActor class in the level:
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
FVector2D ScreenLocation;
PlayerController->ProjectWorldLocationToScreen(WorldLocation, ScreenLocation);
int32 ScreenWidth = 0;
int32 ScreenHeight = 0;
PlayerController->GetViewportSize(ScreenWidth, ScreenHeight); // This works
int32 ScreenX = (int32)ScreenLocation.X;
int32 ScreenY = (int32)ScreenLocation.Y; // This does not
The ScreenX and ScreenY values return nonsense values when printed - I assume from uninitialized memory access. The WorldLocation FVector that I’m feeding this is fine (I use it elsewhere). I don’t think the issue is the playercontroller, as it gives me the correct viewport size, but I could be wrong.
Any thoughts? Or a better way to do this?