Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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()
 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->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;
     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!

Product Version: UE 4.19
more ▼

asked Aug 10 '18 at 02:39 PM in C++ Programming

avatar image

10 1 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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!

more ▼

answered Aug 10 '18 at 05:35 PM

avatar image

1.2k 4 5 8

avatar image guicaisa Aug 12 '18 at 04:37 AM

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

avatar image guicaisa Aug 12 '18 at 05:19 AM

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. alt text

alt text

Your answer really make it works! Thanks again!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question