Download

Showing a UMG userwidget using c++?

can anyone help me to make a pause screen?

I set up a button binding so that ‘P’ on the keyboard triggers a OnPaused function, it works i tested it
and next I created a userwidget with the word “paused” on it in the UMG editor
but i’m stuck now, don’t know how to display it on the screen. i’m trying to do it entirely in c++ code.

once i can figure out how to display the menu, i can try to make it actually pause the game, i saw some virtual void functions exist for pausing. maybe that part will be easy? not sure how pausing might affect replication on multiplayer though.

In essence you will want to create the UUserWidget and add it to the viewport:

UUserWidget* MenuWidget = CreateWidget<UUserWidget>(GetWorld(), Menu);
if(!MenuWidget->IsInViewport())
    MenuWidget->AddToViewport();

And remove the widget when it is no longer needed:

MenuWidget->RemoveFromViewport();

I have a lot going on in my code so I am trying to simplify it here. In my game mode class I have a UPROPERTY TSubclassOf< UUserWidget > that I pass in to a function which creates the widgets for me:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameMenu")
TSubclassOf<UUserWidget> GameMenu;

During the game mode BeginPlay I bind Start and Back to functions that will pause the game and show the menu; resume the game and hide the menu as needed:

APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
if (PlayerController)
{
    EnableInput(PlayerController);

    InputComponent->BindAction("Start", EInputEvent::IE_Pressed, this, &AMyGameMode::Start).bExecuteWhenPaused = true;
    InputComponent->BindAction("Back", EInputEvent::IE_Pressed, this, &AMyGameMode::Back).bExecuteWhenPaused = true;
}

And when showing the game menu:

GetWorld()->GetFirstPlayerController()->bShowMouseCursor = true;
GetWorld()->GetFirstPlayerController()->SetPause(true);
GetWorld()->GetFirstPlayerController()->SetInputMode(UiInputMode);

// call the function (or add code here) to create the UUserWidget and add it to the viewport

And hide the game menu and resume playing:

GetWorld()->GetFirstPlayerController()->bShowMouseCursor = false;
GetWorld()->GetFirstPlayerController()->SetPause(false);
GetWorld()->GetFirstPlayerController()->SetInputMode(GameInputMode);

// remove the widget from the viewport

This does work in 4.27. As to how I implemented it. . . your mileage may vary.

Thanks so much for explaining this, I got the pause screen showing up now and the game does pause, but the game appears to stop taking input events once the game is actually paused. I don’t see the debug message when attempting to unpause so the function isn’t reached. Do you see anything wrong in my code?

void AReptileCharacter::OnPause()
{
	GEngine->AddOnScreenDebugMessage(-1, 12.f, FColor::White, FString::Printf(TEXT("OnPause Function Reached.")));


	if (isPaused)
	{
		GEngine->AddOnScreenDebugMessage(-1, 12.f, FColor::White, FString::Printf(TEXT("UNPAUSING...")));
		if (PauseMenuWidget->IsInViewport())
			PauseMenuWidget->RemoveFromViewport();

		GetWorld()->GetFirstPlayerController()->bShowMouseCursor = false;
		GetWorld()->GetFirstPlayerController()->SetPause(false);
		GetWorld()->GetFirstPlayerController()->SetInputMode(FInputModeUIOnly());

		isPaused = false;
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 12.f, FColor::White, FString::Printf(TEXT("PAUSING...")));
		if (!PauseMenuWidget->IsInViewport())
			PauseMenuWidget->AddToViewport();

		GetWorld()->GetFirstPlayerController()->bShowMouseCursor = true;
		GetWorld()->GetFirstPlayerController()->SetPause(true);
		GetWorld()->GetFirstPlayerController()->SetInputMode(FInputModeGameOnly());

		isPaused = true;
	}
}

Looks like you have your input mode’s backwards.

I don’t think it’s backwards? I check the boolean to see if it’s already paused and I attempt to unpause the game and set isPaused to false since it won’t be paused anymore.

I googled for why unpausing may not work, some people say FInputModeUIOnly() causes all keyboard input to be ignored. I tried using FInputModeGameAndUI() mode but that doesn’t seem to work either. I’m not finding anything about it in the api docs.

So when you pause you’re setting your Input Mode to Game Only, it should be UI only when you’re paused.

Edit: Or GameAndUI

Ah, I misread the first post. Yeah, GameAndUI is what you’d want for button press. Odd that it’s not working.