How does a plugin get a project-agnostic hud to display a widget to in C++?

112 Views Asked by At

I'm making a plugin that I want to display a widget to the user. All the examples I see of creating a C++ widget, and adding it to the viewport, require modifying the game mode, or player character, or something that I wouldn't have access to, being a plugin.

How do I go about getting the HUD and adding a widget to the viewport without knowing the specific hud class in c++?

I've tried several different tutorials and youtube videos, but this is a plugin, so I can't change the player controller, which all of them do.

1

There are 1 best solutions below

0
MashedPotato6587 On

Solved:

#include "Engine.h"
#include "Blueprint/UserWidget.h"

const int PlayerIndex = 0;
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex))
{
    if (UUserWidget* CreatedWidget = CreateWidget<UUserWidget>(Player, MyWidgetClass))
    {
            CreatedWidget->AddToViewport();
    }
}