Where do I set bShowMouseCursor in the PlayerController class such that my game properly uses it? I know I’ve properly setup the game to use MyPlayerController after testing with a debug msg, but I can’t seem to get the mouse cursor to hide in-game (nor click events to register).
From MyPlayerController.cpp :
#include “MyPlayerController.h” AMyPlayerController::AMyPlayerController() { bEnableClickEvents = true; bShowMouseCursor = false; }
From MyPlayerController.h :
#include “CoreMinimal.h” #include “GameFramework/PlayerController.h” #include “MyPlayerController.generated.h” /** * */ UCLASS() class BIRDGAME2_API AMyPlayerController : public APlayerController { GENERATED_BODY() public: AMyPlayerController(); };
reference post: On mouse click event won't work - Blueprint Visual Scripting - Unreal Engine Forums
We use this function in the Base Player Controller specifically to show/hide the mouse cursor. It is called whenever the PlayerController changes state. For example the player was walking and then brought up a menu. Or another example, the player changes what pawn they are controlling.
/**
* Show/hide the Unreal mouse cursor.
*/
void ABasePlayerController::UpdateMouseCursor()
{
PlayerStateType stateType = GetCurrentPlayerStateType();
LOG("ABasePlayerController::UpdateMouseCursor() %s", *GetStringForEnum(stateType));
// never allow click or mouse events for unreal actors or components
bEnableClickEvents = false;
bEnableMouseOverEvents = false;
if (GetWorld())
{
switch (GetCurrentPlayerStateType())
{
// paused - UI only
// interacting - game and UI
case PlayerStateType::PAUSED:
case PlayerStateType::INTERACTING:
{
FInputModeGameAndUI mode;
mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
SetInputMode(mode);
bShowMouseCursor = false;
DefaultMouseCursor = EMouseCursor::None;
CenterMouseOnScreen();
break;
}
// walking - game only
case PlayerStateType::WALKING:
{
FInputModeGameOnly mode;
SetInputMode(mode);
bShowMouseCursor = false;
DefaultMouseCursor = EMouseCursor::None;
break;
}
// main menu - UI only
case PlayerStateType::MAIN_MENU:
{
FInputModeGameAndUI mode;
mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
SetInputMode(mode);
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Hand;
CenterMouseOnScreen();
break;
}
// spectator - game only but show mouse
case PlayerStateType::SPECTATOR_PAUSED:
case PlayerStateType::SPECTATOR_FIRST_PERSON:
case PlayerStateType::SPECTATOR_THIRD_PERSON:
case PlayerStateType::SPECTATOR_FREE_ROAM:
{
FInputModeGameAndUI mode;
mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
SetInputMode(mode);
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Hand;
CenterMouseOnScreen();
}
default:
{
LOG("ABasePlayerController::UpdateMouseCursor() invalid state type: %s", *GetStringForEnum(stateType));
break;
}
}
}
}