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.
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!
answered Aug 10 '18 at 05:35 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here