Get Name or ID from dynamically created Button widget?

Hello there,
I have a widget, that contains only a button. On a event this button gets added (multiple times) to another widget, into a horizontal box. So far so good. The problem now is to "talk" to the different buttons and call different events. For now they all do the same thing, thats called in the original button widget.

Any ideas?

1 Like

Can I ask: do you want the other non-button widget with the horizontal box to execute different functions depending on which button was pressed?

If that’s the case, what I’d do is add an event dispatcher to the button widget, which is called when the button is clicked. The event dispatcher should take an enum as a parameter, and the button widget should have an enum variable on it, which it feeds into the event dispatcher call.

Next, make sure when you spawn the button widgets you set the enum parameter to something to distinguish each button from each other. For example, if you had two buttons, one for “Quit Game” and one for “Open Level”, then I’d allow the enum to have values like E_QuiteGame and E_OpenLevel, and I’d make sure that when I spawned the Open Level button, I set the enum on it to E_OpenLevel.

Next step: in the horizontal box widget, when you add each button, bind an event to the event dispatcher in the button. This new event will need to take an enum parameter.

Now what will happen is:

  1. Button OpenLevel is spawned, with OpenLevel as the enum
  2. Button OpenLevel is added to the horizontal box and an event is bound to the event dispatcher
  3. The button is clicked by the user - the OnClick event fires, calling the event dispatcher with the enum E_OpenLevel
  4. The horizontal box event catches the event dispatcher call, and the event with the enum parameter fires

So hopefully you can see that if you add a switch on the enum in the event, you could execute different functions.

This is not perfect, because the function logic for the button press actually occurs in the horizontal box widget, not the buttons, but it’s basically what I’ve been doing.

Also, here’s some documentation on event dispatchers if you haven’t used them yet.