Widget Not Being Added to the Viiewport via C++

I cannot get my healthbar to display in the viewport no matter what I try, cpp

file void UHealth::NativeConstruct()
{

Super::NativeConstruct();

if (IsValid(HealthWidgetClass))
HealthWidget = CreateWidget<UHealth>(this, HealthWidgetClass);
HealthWidget->AddToViewport(1);
HealthWidget->SetVisibility(ESlateVisibility::Visible);

.h

UCLASS()
class UHealth : public UUserWidget
{
GENERATED_BODY()

private:

UPROPERTY()
	UUserWidget* HealthWidget;

protected:

virtual void NativeConstruct() override;



UPROPERTY(EditDefaultsOnly)
	TSubclassOf<UHealth> HealthWidgetClass;

Have you debugged it and confirmed that HealthWidgetClass isnt coming in as Null?

Everytime I run into this issue its because I forget to select the proper file via the Subclass dropdown in the editor.

sorry if this is a dymb question but how would I go about doing that?

Dumb questions are perfectly fine if you learn from them.

See this post here where they discuss debugging in C++

I knew bits and pieces but that really helped, I will watch the full video later, the breakpoint isn’t being tripped/triggered so I am going to assume it’s null but will continue testing.

Edit: ok so after further testing it seems that the native construct isn’t even running or there is some other issue because breakpoints aren’t firing and logs aren’t being logged, I don’t know 100% if its null as of yet but it seems like it might be another issue and/or both.

when adding a breakpoint in my header file on TSubclassOf ect. I found out that my health widget and health widget class are both null

It sounds like from your previous post that they cpp code isn’t running at all

Edit: ok so after further testing it seems that the native construct isn’t even running or there is some other issue because breakpoints aren’t firing and logs aren’t being logged, I don’t know 100% if its null as of yet but it seems like it might be another issue and/or both.

Something I have run into a few times and you might want to check is that you are parenting the blueprint portion of your widget to the C++ class you have setup.

If it is parented and running properly on the CPP side not just the header side and you are still getting a null for your health widget I would double check the BP and make sure you have the HealthWidget class selected in the appropriate dropdown

thanks and I will get on that and the health widget is already selected in the dropdown, edit: also its parent class is the C++ code

I officially have run out of ideas on what I could possibly do to fix this, the bp is parented correctly and the slot is occupied with the correct thing yet I’m still having issues, the cpp file isn’t running at all from what I can tell and both widgets are null


if you have any more ideas I am all ears because I have no clue what to do next to get this to work

Here is what will solve this for you the best.

Go through your code logically step by step.
Start at the end where you are having your problem
ex Where your nulls are and go backwards
What is calling them. place a breakpoint if that isn’t getting hit then go back further what is supposed to be calling that? place a breakpoint there.

Rinse and repeat until you find the break in the code.

I had this the other day where everything was setup except where I placed a function call on the wrong line and it wasn’t getting called in time. so it never worked.

ok I will do this and thanks for all the help you have given, you are a lifesaver!