Hello Everyone, I’m trying to create buttons at runtime and add them to a horizontal box. When i create those buttons, i would like to bind a function to them on Click. How can i do this? There doesn’t look to be any BP Node that deals with binding events to buttons.
Edit: I came up with a solution a while ago, and forgot to update this.
What i ended up doing was creating widgets for each individual button i wanted, and in the widget, i bound the click to the function. Then, i added a field to all of my building classes called UserButtons, which is an array. On initialization, i populate the array with the buttons I want that building to have. So, when i select my building, the buttons populate the UI.
I’m still learning UMG as we speak, but I believe you would create these buttons as Widgets. Within the buttons you would set up the functionality. Now back in the main Widget, you call Create Widget with your button class you configured and add them to the horizontal box that you have made a variable.
I can show you if you want later, I’m multitasking atm!
That sort of helps. How can i pass it a function to perform when clicked? My user interface already has a reference to the object that is performing the function, i just need to hook it up to the run-time button.
I’m going to add my way of doing it since you haven’t marked this as answered.
The feature I wanted to implement was character portraits. Basically, every player gets a portrait of the other connected players (plus himself) in the HUD; which meant I had to add these portraits runtime. I had the same issue you have with binding stuff during runtime, so my workaround is the following (for my problem);
In the HUDInterface create an array which holds FPortraitLinker.
When a player connects, add all currently connected players to his hud with CreateWidget, and then create an instance of FPortraitLinker, set mPlayerState to the connected player’s state and the mPortraitWidget with the result from CreateWidget, add it to the FPortraitLinker array.
Now you have a link between the Interface and the PlayerState, so you can have a crude binding between the two.