Download

add buttons in horizontal box dynamically?

Hi

Could someone tell me how can I add multiple buttons dynamically (via blueprint) in a horizontal box, and edit their settings, such as the size, onpressed image, the name of the button etc… for each of those buttons

Thank you.

Can anyone help me please.

My auto-save plugin’s demo project has something like this in the main menu map.
The demo is free, there’s a link in the Marketplace page to download it…

Anyway, all you have to do is create a custom widget and spawn it inside the buttons container, using a “for-loop”.

Well I would say, create yourself a Struct with the Data for one Button. So the size, image etc.
Now, in the Widget (or where you can access it) that has the Horizontal Box, create a Variable of that Struct
and make it an Array.

Then fill it with as many entries as you want to have buttons.
Now create a second Widget that only has your button (and maybe text or so) in it.

Use a for each loop on the Struct Array and on each loop body call, you create the Button Widget.

Now you would only need a Function in the Button Widget that takes that struct that you created as an Input and
set the data. So you can pass the Struct data from the Array :stuck_out_tongue:

One unfortunate thing with UMG is that you cannot destroy widgets after you create them. So Your idea is just memory leak, number of buttons will grow over time and unless game is restarted they will stay in memory.
But on other hand creating them dynamically is simple variant and first step to making what i have in mind.
For final version of game, you should estimate maximum number of buttons that can appear on screen, and allocate them at hud creation. Then dynamically manage their look and functionality.
For learning and prototyping you can create them dynamically without worrying about inability to destroy widgets.

That is incorrect. Widgets are garbage collected if nothing is keeping them alive - just like any UObject. If he removes them from the parent, or the parent is removed and nothing keeps it alive they’ll go too. If you’re aware of a particular case where this is not true, let me know. There was an issue back in the 4.10 days where widgets crafted at design time wouldn’t be collected if removed, but that has since been fixed.

Ha, great to know this, I assumed they stay forever in memory since there is no destroy method for them.
It is hard to keep up with all changes in unreal. :wink:

You just gota get the patented Unreal Go brain implant, it’s a direct feed of github checkins to the brain.

Thank you eXi!

I followed your steps and the buttons work like a charm!

If an actor has an event bound to a dispatcher in a widget, does that keep it from being garbage collected?
Or just references keeps it alive? I just want to be sure I don’t have a bunch of widgets hanging around.