Which class do you call within the PlayerController to find the size of the screen, and the current mouse position for the player. I have scavenged through the Strategy Game Template, but I cannot seem to find it.
The goal is to set it up so that when the player’s mouse moves toward the edges the player turns. Here is the code snippet which is called every tick.
// This function Calculates the position of the Mouse
// in the player viewport, and adjusts the player rotation
// when the mouse cursor gets close to the endge of the screen.
// Call the HUD
FViewport* const Viewport = GEngine->GameViewport->ViewportFrame->GetViewport();
ACharacter* Character = GetCharacter();
// This value must remain between 0 and 1.
float UserSetScale = 1.f;
FIntPoint MousePos; // Mouse Poisition
// Get Viewport Mouse Position
Viewport->GetMousePos(MousePos);
float ScreenSizeX = Viewport->GetSizeXY().X; // Screen Size X
float ScreenSizeY = Viewport->GetSizeXY().Y; // Screen Size Y
// These values are scaled to be used in player input
// Value Between: 0 and 1
// Math Formula:
//
// MouseScalar = ((MousePosition - TheCenterofScreen) / (TheCenterofScreen)) * UserSetScale
//
float MouseXScalar = ((MousePos.X - (ScreenSizeX / 2)) / (ScreenSizeX / 2)) * UserSetScale;
float MouseYScalar = ((MousePos.Y - (ScreenSizeY / 2)) / (ScreenSizeY / 2)) * UserSetScale;
// Update Player Input with New Values
Character->AddControllerYawInput(MouseXScalar);
Character->AddControllerPitchInput(MouseYScalar);
Just to clarify, I am having issues with the viewport on line 6. It has all the data I need, but I think that it is referencing a viewport that does not exist. I have attempted to call the HUD, but it does not have the data I need. I do not know which class I should be using instead of the GEngine viewport.
I updated the code, but the viewport is saying that the screen is X=2,Y=0.
// This function Calculates the position of the Mouse
// in the player viewport, and adjusts the player rotation
// when the mouse cursor gets close to the endge of the screen.
const ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(Player);
// Find the Viewport Size
FIntPoint ViewportSize = LocalPlayer->ViewportClient->Viewport->GetSizeXY();
ACharacter* Character = GetCharacter();
// This value must remain between 0 and 1.
float UserSetScale = 1.f;
float MousePosX; // Mouse Poisition X
float MousePosY; // Mouse Poisition Y
// Get Viewport Mouse Position
GetMousePosition(MousePosX, MousePosY);
float ScreenSizeX = ViewportSize.X; // Screen Size X
float ScreenSizeY = ViewportSize.Y; // Screen Size Y
// These values are scaled to be used in player input
// Value Between: 0 and 1
// Math Formula:
//
// MouseScalar = ((MousePosition - TheCenterofScreen) / (TheCenterofScreen)) * UserSetScale
//
float MouseXScalar = ((MousePosX - (ScreenSizeX / 2)) / (ScreenSizeX / 2)) * UserSetScale;
float MouseYScalar = ((MousePosY - (ScreenSizeY / 2)) / (ScreenSizeY / 2)) * UserSetScale;
// Update Player Input with New Values
Character->AddControllerYawInput(MouseXScalar);
Character->AddControllerPitchInput(MouseYScalar);
Thank for you help finding that code, I knew it was in the player controller somewhere.
It is weird that I cannot Call GetMouseScreenPosition since all of this code is in the Player controller. I attempted to see if the function was an extended function of the AStratageyController class, but it is not. I do not know where they got it from.