Blueprints - Only run code when widget is actually shown

Hey, I am working on a RTS where I have a bunch of selected unit icons. My goal is to only update the health bar when a event fires that the unit health updated.

The problem is UMG is constructing selection widgets I didn’t request and then throwing errors because the unit is not set.

Clearly I can’t trust the “On Construct” event. But it seems widgets don’t have an event for when the widget is actually “used” (displayed). Do I HAVE to use c++?

we’d need to see your code

random widgets wont just be created, you can use visibility to check if its shown, but then what decides if its shown or not?

The only place I can find that creates an instance of that widget is this. I added logging to confirm it only fires once when a unit is selected. This happens in a widget that acts as the selection icon container.

My best suspicion is this array property that holds all my currently created selection icons. The Blueprint debugger says the selection icons are being created when my WGT_MainUI is being constructed. I think the array is being pre-filled with “default” icons.

WGT_MainUI
---->WGT_SelectedUnitsArea
[some c++ code initializes array with default icon?]

so you likely dont want to create the widget everytime a unit is selected, if that is what you want then you need to ensure that you remove the old widget hence the duplicates.

if you only have 1 unit selected at a time you can save the WidgetRef and use a validated Get. if invalid create the widget, if valid just update the existing widget

the way i do it is i’d have a main HUD widget, inside that i’d have a container say a horizontal box. inside that i’d create X amount of SelectedUnit widgets with visibility set to collapsed. then i’d just update those widgets and make them visible if i have a valid selected unit

The selection event hasn’t even run though, so where are the widgets coming from? Constructing a WGT_MainUI shouldn’t make any instances of WGT_SelectedUnit until I have selected a unit. The only place I have calling create WGT_SelectedUnit is when a unit is selected.

These WGT_SelectedUnit instances are being created right at the start of the game. Some magic C++ code is automatically making these widgets. 8 WGT_SelectedUnit instances were created before my On Selected event had fired.

R.e. creating widgets every time a unit is selected, I read somewhere that the underlying slate resources are re-used (ie. there is already pooling of the widgets) so I’m going with that for now.

did you have a PreConstruct Node doing it for preview?

Nope, no pre-construct. The blueprint debugger just tells me that some c++ is creating the unit selection widges when I call this:

However WGT_MainUI has no reference to WGT_SelectedUnit. WGT_MainUI contains WGT_SelectedUnitsArea, but WGT_SelectedUnitsArea doesn’t add a WGT_SelectedUnit until a unit is selected.

This is very mysterious. My workaround is just to have a check in my widget logic if the selected entity is set.

Well… turned out I was exceptionally silly. In my minimap (c++) I was spawning the selected unit widget instead of the minimap icon. They look exactly the same when zoomed out, without a portrait :man_facepalming:

Side note: the widget was being referenced in a custom config which was why “find all references” didn’t show it being used.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.