Add simple progressbar to my C++ HUD

Since the blueprint is 100% C++, how do I now add HUD-components like progressbars to it? I can’t even get the right header it seems even though this should be the one according to: UProgressBar | Unreal Engine Documentation

The solution must be insanely simple.

...
//#include "Runtime/UMG/Public/Components/Widget.h"
#include "Runtime/UMG/Public/Components/ProgressBar.h"
#include "CanvasItem.h"

AFPS1HUD::AFPS1HUD()
{
	// Set the crosshair texture
	static ConstructorHelpers::FObjectFinder<UTexture2D> CrosshiarTexObj(TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair"));
	CrosshairTex = CrosshiarTexObj.Object;

	UProgressBar EnemyHPBar; // <<<< identifier UProgressBar  is undefined
}

I did find a way to manually draw bars by using “Canvas->DrawIcon” but I prefer using the widgets already provided if possible.

Hi Napoleon,

Make sure you have added “UMG” to the list of public dependencies in your [ProjectName].build.cs file. Let me know if that doesn’t work for you and I’ll look into it further.

Best,

Cody

I added it but it didn’t change anything. I’m not sure if this is even supported with C++ or if it is blueprints only.

using UnrealBuildTool;

public class FPS1 : ModuleRules
{
	public FPS1(TargetInfo Target)
	{
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });
	}
}

UMG is essentially a wrapper for Slate that allows us to build out widgets in the editor with blueprints. If your intention is to work solely in C++, you’ll want to create a Slate progress bar instead of a UMG progress bar. Alternatively, you can create your HUD widget in UMG through the editor and write your own HUD class to attach it to the viewport.

Okay thanks I see. I tried using a SlateWidget instead. But JUST this line alone will cause VS to fail to compile:

#include "SProgressBar.h"

The list of errors was huge this is only a section of it. I also tried:

#include "Runtime/Slate/Public/Widgets/Notifications/SProgressBar.h"

But with the same result.
Also SNew is an unidentified identifier. Am I missing another project reference? Note that putting the 2 referenes in the public section also does not work.

// VS doesn't recognize SNew...
SProgressBar MyUIWidget = SNew(SProgressBar).OwnerHUD(this); 

My Build.cs file:

public class FPS1 : ModuleRules
{
	public FPS1(TargetInfo Target)
	{
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
		PrivateDependencyModuleNames.AddRange(new string[] {"Slate", "SlateCore" });
	}
}

You’ll want to include “SlateBasics.h”, hopefully that will clear up your errors. Additionally, I would suggest creating your own custom slate widget to add the progress bar to, and adding that custom widget to your HUD.

It may be a bit out of date, but there’s a useful guide here: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

Thanks, adding “SlateBasics.h” fixed all compiler errors.

The tutorial you linked was up2date but had 2 minor mistakes that prevented it from compiling (probably a typo):

#include "StandardSlateWidget.h"

Should be (at 2 places):

#include "SStandardSlateWidget.h"

After that you should get the “Hello, Slate!” in red text at the top of the viewport as intended. Also note that you have to close VS and RMB on your .uproject and click “Generate Visual Studio Files”. Otherwise VS may not recognize the changes.

But I still have no real idea of how to add a simple SProgressbar using slatewidgets. Everything I try so far raises errors.
Also I welcome a more detailed tutorial (preferably video) explaining slatewidgets into detail. I find loads of UMG/Blueprint tutorials but not a single usable one for slatewidgets in C++.

Perhaps I’ll go with your alternative solution you mentioned earlier. It seems much much more simpler and accomplishes the same:

Alternatively, you can create your HUD widget in UMG through the editor and write your own HUD class to attach it to the viewport.