Which class do I call to get the current screen of the player from PlayerController

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);

Thank you in advance. :slight_smile:

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.

Look in the StrategyPlayerController.cpp more specificly in

void AStrategyPlayerController::ProcessPlayerInput(const float DeltaTime, const bool bGamePaused)
{
	if (!bGamePaused && PlayerInput && InputHandler && !bIgnoreInput)
	{
		InputHandler->UpdateDetection(DeltaTime);
	}

	Super::ProcessPlayerInput(DeltaTime, bGamePaused);
		
	if (!bIgnoreInput )
	{
		const ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(Player);
		AStrategySpectatorPawn* StrategyPawn = GetStrategySpectatorPawn();		
		if(( StrategyPawn != NULL ) && ( LocalPlayer != NULL ))
		{
			// Create the bounds for the minimap so we can add it as a 'no scroll' zone.
			AStrategyHUD* const HUD = Cast<AStrategyHUD>(GetHUD());
			AStrategyGameState const* const MyGameState = GetWorld()->GetGameState<AStrategyGameState>();
			if( (MyGameState != NULL ) && ( MyGameState->MiniMapCamera.IsValid() == true ) )
			{
				if( LocalPlayer->ViewportClient != NULL )
				{
					const FIntPoint ViewportSize = LocalPlayer->ViewportClient->Viewport->GetSizeXY();
					const uint32 ViewTop = FMath::Trunc(LocalPlayer->Origin.Y * ViewportSize.Y);
					const uint32 ViewBottom = ViewTop + FMath::Trunc(LocalPlayer->Size.Y * ViewportSize.Y);

					FVector TopLeft( HUD->MiniMapMargin, ViewBottom - HUD->MiniMapMargin - MyGameState->MiniMapCamera->MiniMapHeight, 0 );
					FVector BottomRight( (int32)MyGameState->MiniMapCamera->MiniMapWidth, MyGameState->MiniMapCamera->MiniMapHeight, 0 );
					FBox MiniMapBounds( TopLeft, TopLeft + BottomRight );
					StrategyPawn->GetStrategyCameraComponent()->AddNoScrollZone( MiniMapBounds );
					StrategyPawn->GetStrategyCameraComponent()->UpdateCameraMovement( this );
				}
			}
		}		
	}
}

screen stuff are related to viewport.

For the mouse position check out the StrategyInput.cpp .

in UpdateGmeKeys the mouse is get for somes check in

void UStrategyInput::UpdateGameKeys(float DeltaTime)
{
	AStrategyPlayerController* MyController = Cast<AStrategyPlayerController>(GetOuter());
	check(MyController);

	// gather current states

	// Look at mouse input
	bool bCurrentLMBState = false;
	FVector2D MouseScreenPosition;
	if (MyController->GetMouseScreenPosition(MouseScreenPosition))
	{
		bCurrentLMBState = MyController->PlayerInput->IsPressed(EKeys::LeftMouseButton);

		// detection - mouse is already in window local coords
		DetectOnePointActions(bCurrentLMBState, bPrevLMBState, DeltaTime, MouseScreenPosition, MouseAnchor, LMBDownTime);
	}
....

if you need more info just says it.

gamer08

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.

Do not forget for the screen mouse position there is a process of deprojection. So check in FStrategyHelpers or around this function

void AStrategyPlayerController::OnSwipeUpdate(const FVector2D& ScreenPosition, float DownTime)
{
    AActor* const Selected = SelectedActor.Get();
    if ( Selected && Selected->GetClass()->ImplementsInterface(UStrategyInputInterface::StaticClass()) )
    {
       ULocalPlayer* const MyPlayer =  Cast<ULocalPlayer>(Player);
       const FPlane GroundPlane = FPlane(FVector(0, 0, SelectedActor->GetActorLocation().Z), FVector(0,0,1));

       FVector RayOrigin, RayDirection;
       FStrategyHelpers::DeprojectScreenToWorld(ScreenPosition, MyPlayer, RayOrigin, RayDirection);
       const FVector ScreenPosition3D = FStrategyHelpers::IntersectRayWithPlane(RayOrigin, RayDirection, GroundPlane);

       IStrategyInputInterface::Execute_OnInputSwipeUpdate(Selected, ScreenPosition3D - SwipeAnchor3D);
    }
    else
    {
       // move around the map
       FVector2D ScreenDelta = ScreenPosition - PrevSwipeScreenPosition;
       GetStrategySpectatorPawn()->GetStrategyCameraComponent()->MoveSwipe(ScreenDelta);
    }

    PrevSwipeScreenPosition = ScreenPosition;
}

as you can see there is a screenPosition that is grab somewhere. Just follow the stack.

For the screen size it’s within the viewport but you say it’s return 2 in x and y 0 ? strange

maybe take a look at APlayerController::GetViewportSize()

if ther is anything else say it.

gamer08

Thank you, I have a question. Is deprojection still necessary if the game is in First Person?