Using a UMG HUD with c++ game mode and player character

Hi,

I am starting to use Unreal Engine, and I wish to use a HUD for a small game.

I have seen that using UGM is easier to draw HUDs, but the rest of my project is in C++
I can’t find how to use my blueprint HUD which is a UUserWidget with my C++ project.

While searching, I found this link : A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums.

I used it to create my own subclass of UUserWidget, but now, I don’t know how to get this widget to actually be used by my C++ HUD subclass.

Thank you.

You can use CreateWidget and AddToViewport() to display your widget. Consider this setup (the code below might contain some syntax errors because I don’t have any docs or Visual Studio at the moment to check it, but you get the idea):

.h file

TSubclassOf<YourCustomWidget> YourCustomWidgetUIClass;

YourCustomWidget* YourCustomWidgetUIWidget;

.cpp file

YourCustomWidgetUIClass = LoadClass<YourCustomWidget>(..., TEXT(path_to_your_widget_in_content_browser), ...);

YourCustomWidgetUIWidget = CreateWidget<YourCustomWidget>(GetWorld(), YourCustomWidgetUIClass);
YourCustomWidgetUIWidget->AddToViewport();

“path_to_your_widget_in_content_browser” can be something like “Game\UI\MyWidget.MyWidget_C”.

Hope this helps :slight_smile:

Thanks, I might not be able to try this right away, but i’ll keep you updated as soon as possible !

What if we don’t want to use any blueprint widget? So every widget is created purely in C++. How can we do that?