So we have already discussed how to add a widget to the viewport, which means that it will be rendered on the player's screen.
However, what if we want to have UI elements that are toggled based on other factors, such as proximity to certain Actors, or a player holding a key down, or if we want a UI that disappears after a specified time?
Create a new
GameMode
class calledToggleHUDGameMode
.Override
BeginPlay
andEndPlay
.UPROPERTY() FTimerHandle HUDToggleTimer;
Lastly add this member variable:
TSharedPtr<SVerticalBox> widget;
Implement
BeginPlay
with the following code in the method body:void AToggleHUDGameMode::BeginPlay() { Super::BeginPlay(); widget = SNew(SVerticalBox) + SVerticalBox::Slot() .HAlign(HAlign_Center) .VAlign(VAlign_Center) [ SNew(SButton) .Content() [ SNew(STextBlock) .Text(FText::FromString(TEXT("Test button"))) ] ]; GEngine->...