c++ create UProgressBar but set image failed

I try to create a UProgressBar by cpp. I have already create my userwidget, and i successfully create a uprogressbar. Then i want to do more about it, suck as set background image and the fill image, i know that is very simple to do with blueprint, but i also want to try it by cpp.

But i find there is always a crash when i set a UTexture to the UProgressBar.

UCharacterUserWidget::UCharacterUserWidget(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	static ConstructorHelpers::FObjectFinder<UTexture> HT(TEXT("/Game/HUDAssets/Health_NoFill"));
	HealthText = HT.Object;
}

void UCharacterUserWidget::NativeConstruct()
{
	Super::NativeConstruct();
}

TSharedRef<SWidget> UCharacterUserWidget::RebuildWidget()
{
	UCanvasPanel* RootWidget = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), TEXT("RootWidget"));
	UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);

	if (RootWidgetSlot)
	{
		RootWidgetSlot->SetAnchors(FAnchors(0.0f, 0.0f, 1.0f, 1.0f));
		RootWidgetSlot->SetOffsets(FMargin(0.0f, 0.0f));
	}
	WidgetTree->RootWidget = RootWidget;

	TSharedRef<SWidget> Widget = Super::RebuildWidget();
	//
	HealthProgressBar = WidgetTree->ConstructWidget<UProgressBar>(UProgressBar::StaticClass(), TEXT("HealthProgressBar"));
	HealthProgressBar->RenderTransform.Translation.X = 160.0f;
	HealthProgressBar->RenderTransform.Translation.Y = 70.0f;
	
	HealthProgressBar->BackgroundImage_DEPRECATED->Brush.SetResourceObject(HealthText);
	//HealthProgressBar->RenderTransformPivot.X = 0.0f;
	//HealthProgressBar->RenderTransformPivot.Y = 0.0f;
	
	//static ConstructorHelpers::FObjectFinder<UTexture> HealthFillBrush(TEXT("/Game/HUDAssets/Health_NoFill"));
	//static ConstructorHelpers::FObjectFinder<UTexture2D> HelathFillBursh2D(TEXT("/Game/HUDAssets/Health_NoFill"));
	
	//HealthProgressBar->FillImage_DEPRECATED = HealthFillBrush.Object;

	//
	ManaProgressBar = WidgetTree->ConstructWidget<UProgressBar>(UProgressBar::StaticClass(), TEXT("ManaProgressBar"));
	ManaProgressBar->RenderTransform.Translation.X = 160.0f;
	ManaProgressBar->RenderTransform.Translation.Y = 110.0f;
	ManaProgressBar->RenderTransform.Scale.X = 340.0f;
	ManaProgressBar->RenderTransform.Scale.Y = 40.0f;
	ManaProgressBar->RenderTransformPivot.X = 0.0f;
	ManaProgressBar->RenderTransformPivot.Y = 0.0f;

	RootWidget->AddChild(HealthProgressBar);
	//RootWidget->AddChild(ManaProgressBar);

	return Widget;
}

I use many ways to load the image asset, such as ConstructorHelpers or LoadObject, but when i call HealthProgressBar->BackgroundImage_DEPRECATED->Brush.SetResourceObject(). It always crash. I don’t know why. I have already decided to use the blueprint extend from cpp…but i really want to know the reason or if there is any method can do more about UProgressbar by cpp. Need your help.Thanks!

My best guess is the call on line 31 of your snippet is throwing a NULLPTR exception and causing a crash because you aren’t first checking to make sure ‘BackgroundImage_DEPRECATED’ is valid.

You should always check the validity of a pointer before trying to perform actions on the pointed-to object, because the object might not even exist!

In this case, though, I think there’s a bigger issue. As hinted by its name, ‘BackgroundImage_DEPRECATED’ appears not to be the current or intended way to accomplish what you’re trying to do. Take a look at the API page for UProgressBar and check out the WidgetStyle variable instead. That variable stores a FProgressBarStyle struct with methods like SetBackgroundImage(…) and others, which seems closer to what you’re looking for.

That said, however, I recommend you try to avoid some headache and not bother with setting the widget background image via code in the first place. I suggest you use your UCharacterUserWidget class to create your custom behaviors (functions) via C++, and then just create a Widget Blueprint in the editor that derives from your UCharacterUserWidget class. This way, you can simply choose your background image by opening up the Widget Blueprint Editor and customizing your widget visually — the way you would with any other User Interface object.

Maybe that last suggestion won’t work for some special cases, and maybe that includes what you’re needing to do. But, generally, I find it a lot easier to use UE’s wonderful interface for the graphical stuff and use C++ primarily to create custom functionality.

Hope this helps!

Thanks for your answer. it inspire me a lot. Thank you very much!

I have a look about the class UProgressBar carefully following your answer. I find the WidgetStyle variable that you metioned is what i looking for. I used the wrong variable before.

I think maybe the variable ‘BackgroundImage_DEPRECATED’ was used in earlier engine version and now it not recommended to use . I find that maybe the best way to find the right variable in cpp class is via the category in the corresponding blueprint detail panel.

250235-tim图片20180812130314.png

Your answer really make it works! Thanks again!