UMG Widget: Tips for collapsing textblocks with null values, is there a better way?

Heres the setup… a bunch (eventually probably like 30-40+) of stats for items in an inventory. Followed AntiquisTheGame’s tutorial to a t but now I’m adding extra functionality. I obviously dont want say an apple to have weapon damage, tool damage, item health, poison rate, etc. showing up in my sizeBox that holds all of these so I got to thinking, whats the best way to hide them.

So my mind of course goes to my bindings for the actual stats, the text, like so…

And I add a setVisibility(Collapsed) to true (no item selected) and the inverse to false. Then I realize two things. A) that wont work, because im checking if I have an item selected, when I need to check if the item has a value. Also, I’ll need to create a variable for every textblock so that I can wire it into the setVisibility() node otherwise the whole inventory is collapsed.

So I then setup a branch on each true and false for, in this pictures case, if weight == 0.0f; (that would mean a null value, unlses someone can think of a better way to check if a float has been set to something otherthan the stock 0.0… its in a struct as you can see) anways if weight == 0.0f; then setVisiblity(collapsed) else setVisiblity(visible)

Like so… but this doesnt work, it remains hidden no matter what.

So my last remaining option is to bind the visibility, which works fine, but its a heck of a load of functions and bindings to create, even if it does mean I dont need the variables.

That works. Any better ideas? I thought about changing it so that for each item in ItemDataStruct addChildWidget(TextBlockWidget) but then I would have to do more figuring out about how to set visiblity up on that, I’m just learning the more intricate parts of UMG now, adding widgets into widgets is new to me.

Any help would be appreciated. Thank you.

Don’t ever change widget properties inside a binding function. Those functions are generated as pure, and you shouldn’t be calling function that have side effects, like changing visibility. You should move that kind of code out of binding statements and just do it in Tick.

I probably would have made a set of library functions either in C++ or blueprint libraries for this, like HasStat(InventoryItemObjectOrStruct, SomeStatEnum), where you have a switch statement that does the right compare operation to determine if that stat is present. Same for visibility, GetStatVisibility(…) that calls HasStat and uses the blueprint Select node to return the right thing, the if is not as nice, since it requires a local variable. Finally I would have a GetStatText()? Maybe, does the same thing as HasStat, but gets the right text formatted version.

Personally - all my inventory objects would be UObjects and these functions would likely just be blueprint exposed C++ functions on my inventory items.

Hi guys. This is how I did mine. I’m no expert though. Using trial and error.
The texts in this widget are controlled by variables. If empty, they will be collapsed.
Works fine for me, but don’t know if it’s the optimal method.

This might be enough if placed inside the widget. No other script necessary. Wrap in a function if it needs to be reused / used for an update.

1 Like