Sorry if this has been asked before. I’ve been trying to figure this out and I can find no apparent solution. So I’m making a character creation screen. I have a data table with all the classes in the game which gets iterated through and a button gets added for each class. My current understanding is the way you add functionality to buttons is through custom events. My problem is when adding custom events in a for loop it doesn’t work. Here are screenshots of how it works now:
This is suboptimal because it means every time I add new classes i need to go back in here and update it with the new class IDs. I’d really prefer to just make it once and have it loop through all the classes, make the buttons and make custom events for each button added which do what I want them to.
This is what I tried, but it obviously isn’t going to work:
Why not just handle this logic in the button itself?
Initialize it with the data that it needs, and then in its own blueprint, on clicked, do what you’re supposed to
Remember there’s nothing stopping you from broadcasting another delegate that has more information, and not rely on OnClicked. So you can bind to a new delegate that has all the information you need, which is broadcast through OnClicked in the button blueprint. That way you can still handle all the logic in the main widget that you’ve shown. For example you can initialize each button with perhaps the whole Character Class Data struct, so then the button itself knows everything about the class it represents, including the ID. Then the button can broadcast a new event (OnClassSelected) for example, with the data it has as a parameter, making it very easy to react to that
This is an excellent suggestion! I was able to solve the problem with this advice. I made a new more specific button widget just for the class. I made a function that accepted all the relevant information and references, and set local variables. Off of the on clicked node in the event graph, I used the local variables I set to perform all of the actions I previously had in the custom events. I got rid of the custom events, and simply called the function to set all the variables inside each button that was created. Here’s what the graphs look like now: (These screenshots aren’t for class, but for a simpler case with the same issue called “Kind” but if it works for this, it will work for the class as well)