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.


