How to set onClick actions for dynamically created buttons in UMG?

I have a widget which is essentially a button with particular styling and common functionality. I then have a UMG widget which contains a few of these buttons.

What I’m wondering is how to set the onClick events of these buttons? The onClick for each button fires a totally different function which is contained in the main UMG widget, so there’s no common functionality which could be put in the button blueprint itself.

One thing I have tried, which does work but seems a bit inelegant, is having each button with a string variable (eg. ‘PLAY’), which can be set in the containing widget upon spawn, and an onClick which sends this string to the main widget as an argument for a function called ‘UI_button_pressed’ which is essentially a switch on string leading to all the specific functionality for each button.

But is there any way to setup the onClick event from within the UMG widget that contains these buttons?

How about:
make enum that selects function you want

make custom event or function in parent widget, that accepts that enum
then that function/event calls selected (by enum) final function

From dynamically created buttons, get owner pointer, cast to owner widget class, call that custom event/function

Your solution was kind of what i am suggesting (only difference that i would use enum).

Edit there is nothing better you can do.
You can move some functionality to parent or to button or inside interface, but ultimately it will be always same structure, just in different places.

Best way would be making events or functions in player controller (or pawn)

then in buttons on event click you make logic that selects what it needs to call dependig on enum.
something like: enum = “start” so get owner get controller, cast to controller, call “event_start”
and so on for all types of that enum and function to call
When you create button you set its enum variable to desired call

This way you have all that garbled logic inside button widget, and once it works you do not need to touch it ever again.

Ps
if you want to make it all more generic (so it works with different classes of pawn), you can make blueprint interface, and then do interface call, and implement all functions inside interface.

This is an older post that deals with dynamically created buttons that eventually talk to a playercontroller. This is the easiest way to do this afaik.