The level in question doesn’t have a player, but there is a PlayerController (input mode: FInputGameAndUI). At no point in the PlayerController script do I hide the mouse.
In this level, the mouse is initially visible, but I would like to hide it after clicking a button.
Things I’ve already tried:
Canvas->Cursor = EMouseCursor::None;
PC->SetShowMouseCursor(false);
PC->bShowMouseCursor = false;
I also did a test where I set CanvasPanel → Advanced → Cursor → None through the widget’s Blueprint in the editor. Then I ran the game, and the mouse was hidden. That’s why I need to know how to hide the mouse from the widget at runtime.
Hey nove0nove0! How are you?
The problem that I can see there is that the Widget has control of the cursor instead of the Player Controller because its input mode is FInputGameAndUI.
One thing you can do to hide the cursor is to implement in the “OnClick” event the code below:
// OnClicked event
UFUNCTION()
void Your_Widget::OnButtonClicked()
{
// Hide Cursor
SetCursor(EMouseCursor::None);
// Your button logic here...
}
Please let me know if that works for you!
1 Like
It didn’t work. I forgot to mention that I’m using Unreal Engine 5.1. I didn’t share the code because it’s very messy — I’m still learning, and everything is very new to me.
If you’d like, I can try to share the project with you (it has some comments in another language, but I can write a summary explaining how everything works).
Hey!
Sorry, but I’m not be able to check your project personally. But I’ve been checking and you can test with a more “extreme” approach haha
You can create a function in your Widget that checks and tries to hide the widget in a couple different levels:
void YourWidget::HideMouseCursor()
// Remember to change "YourWidget" with the name of your widget
{
// Level 1: directly from the widget
SetCursor(EMouseCursor::None);
// Level 2: from the Player Controller, as backup in case level 1 fails
if (APlayerController* MyPlayerController = GetOwningPlayer())
{
PC->SetShowMouseCursor(false);
PC->bShowMouseCursor = false;
}
// Level 3: from the Viewport
if (UGameViewportClient* MyViewportClient = GetWorld()->GetGameViewport())
{
ViewportClient->SetMouseCursor(EMouseCursor::None);
}
}
Then, if you call this function when pressing the button, the cursor should be hidden no matter what is controlling it. And, if it works, you can debug this to check which part of the code is actually hidden the cursor at any given moment.
Hope this helps!
1 Like
Hi, at first I tried to hide the mouse only on the Canvas thinking that it would affect the mouse visibility on the other elements of the widget, but I only got the result I wanted when I changed the mouse cursor on all elements. Either way, thank you very much for your help.
void UStartMenuHUDClass::OptionButtonClick()
{
if (!PC)
GetPC_StartGame();
if (PC)
{
PC->SwitchToKeyboardNav();
PC->bShowMouseCursor = false;
Canvas->SetCursor(EMouseCursor::None);
Overlay->SetCursor(EMouseCursor::None);
VerticalBox->SetCursor(EMouseCursor::None);
StartButton->SetCursor(EMouseCursor::None);
StartButtonText->SetCursor(EMouseCursor::None);
OptionButton->SetCursor(EMouseCursor::None);
OptionButtonText->SetCursor(EMouseCursor::None);
}
}
1 Like