UMG Widget Blueprint focus in C++

I have a problem in my FPP C++ template (4.13). I couldn’t resolve using answers from answerhub so I post it here.

The game runs with cursor locked within viewport boundaries, but I need to click on viewport to be able to focus on the menu widget. At the same time the mouse cursor is not locked to the viewport anymore… WTF?
[RESOLVED by SetInputMode(FInputModeUIOnly) on BeginPlay()]

Another problem occurs after I click ‘New game’ which loads a new map - the cursor stays visible and has no focus on the FPP player controller.

Here’s my setup:

  • Created blank level
  • Created UMG menu widget blueprint (set to focusable)
  • Set game mode to my custom C++ game mode
  • Set player controller to my custom C++ player controller
  • Created and added the widget to viewport in C++
  • Started the game in editor as Standalone Game


    AMainMenuGameMode::AMainMenuGameMode()
        	: Super()
        {
        	PlayerControllerClass = AMainMenuPlayerController::StaticClass();
        
        	HUDClass = AMainMenuHUD::StaticClass();
        
        	static ConstructorHelpers::FClassFinder MainMenuWidgetBlueprint(TEXT("WidgetBlueprint'/Game/FirstPersonCPP/Blueprints/MainMenuWidget'"));
        	
        	if (MainMenuWidgetBlueprint.Succeeded())
        	{
        		MainMenuWidgetClass = MainMenuWidgetBlueprint.Class;
        	}
        	else
        	{
        		MainMenuWidgetClass = nullptr;
        	}
        }
        
        void AMainMenuGameMode::BeginPlay()
        {
        	Super::BeginPlay();
        
        	if (MainMenuWidgetClass != nullptr)
        	{
        		MainMenuWidget = CreateWidget(GetWorld(), MainMenuWidgetClass);
        
        		if (MainMenuWidget != nullptr)
        		{
        			MainMenuWidget->AddToViewport();
        		}
        	}
        }

There is also a third problem when I want to show pause menu while playing TPP level. Menu widget shows up affter AddToViewport(), but the cursor stays invisible and I can’t focus on the pause menu widget.



    void AGamePlayerController::BeginPlay()
    {
    	Super::BeginPlay();
    	bShowMouseCursor = false;
    	SetInputMode(InputModeGameOnly);
    }
    
    void AGamePlayerController::ShowMenuCursor(TSharedPtr MenuWidgetPtr)
    {
    	bShowMouseCursor = true;
    	SetInputMode(InputModeUIOnly);
    	InputModeUIOnly.SetWidgetToFocus(MenuWidgetPtr);
    }
    
    void AGamePlayerController::HideMenuCursor()
    {
    	SetInputMode(InputModeGameOnly);
    
    	bShowMouseCursor = false;
    }

Where and when do you call your SetInputMode functions? You should SetInputMode(FInputModeUIOnly()) on BeginPlay in the main menu level, and in your game level you should SetInputMode(FInputModeGameOnly()) on BeginPlay. And don’t forget to set bShowMouseCursor = false in your HideMenuCursor function.

Ok, so thanks to your solution I’ve managed to resole the problem with no focus on the entry of main menu level. But I’m still struggling with the Pause Menu after loading the new game level. I pause the game with ESC key, and execute AGamePlayerController::ShowMenuCursor method (like it’s shown above) but the pause menu widget has no focus and mouse cursor is not visible.

I this is also the same problem which is not resolved: https://answers.unrealengine.com/questions/138594/focus-issue-when-adding-umg-widget-in-code.html