Daily Unreal Column #51 - Viewport Stats subsystem
There are cases where you want to display a message in a viewport to notify the user that something has happened or is happening. AddOnScreenDebugMessage is not the only available option.
The most often, when people want to display a message in a viewport they use GEngine→AddOnScreenDebugMessage. This is fine, except it’s not ideal for all use cases. For example, what if you want your message to be displayed persistently?
Viewport Stats subsystem comes to the rescue. Using this subsystem you can add a delegate that will define what kind of message and of what color should be displayed in the viewport.
if (auto* ViewportStats = GetWorld()->GetSubsystem<UViewportStatsSubsystem>())
{
ViewportStats->AddDisplayDelegate([](
FText& OutText, FLinearColor& OutColor)
{
OutText = FText::FromString("Hello world");
OutColor = FLinearColor::Red;
return true;
});
}
This is how the above code will look like in the editor once executed.