Download

Dynamically creating and binding events.

I am working on a menu structure that accepts a set of arrays to dynamically create sub-menus, and fills them with Buttons.

All of these buttons are supposed to do very similar actions, with different parameters that are decided on construction.

How can I automatically create and bind Events to the IsPressed of these buttons?

I thought I would give a little more Information.
The game is going to be an RTS, and the menu I am trying to create is the build menu.

So far I have created three Widgets for this. A custom function we created delivers us a Kismet Array with all necessary information about all our building blueprints.

The first Widget creates a Button for every Category, and add them into a Horizontal Box. These are going to function like tabs.
The second widget is filled with Buttons for every Building of that Category, and order them according to Type,

The third Widget is our main HUD, and it creates an instances of the Category Widget, as well as one Building Widget for every Category on Construction.

This is how far the stuff is implemented and working.

Now for the stuff that I do not know how to do:
I want to bind the Category Buttons so that they hide all Building Widgets aside from the one that corresponds to their category.
To do this I need to create an event for every Category Button, bind that event to the right button, and make it execute a function.

Also I want every Building Button to initiate building that Building, when clicked.
Similarly to the Category Buttons, I need to create an event for every Building Button, and bind that to the right function.

The problem I am confronted with is that I cannot bind the OnClicked event of a button that is not yet created,
and I cannot connect my function with an Event that is not yet created.
Also the function needs an input to recognize which Button executes it, or which are its targets.

The “Create Event”-Node does not seem to be what I am looking for sadly,
and neither is an event Dispatcher. At least as far as I have understood the Unreal Documentation.

I can use the Create Event Node to fill an array, but I cannot declare that array as a variable, and I cannot add this array to a map that I can declare a variable.

The only thing I can find is this post from the forums.

It would require me to change my approach a bit, but that is not as much of a problem as the fact that I have no idea what is going on in that example.

The way the Events and Dispatchers are named is really confusing.

There’s a lot of things going on in your second post, it’s difficult to pin down just what you need help with. In your third post, the link you posted is pretty straight forward and fundamental - if you understand event dispatchers. What is happening is a ButtonWidget has a button in it, and that’s all it has, probably along with some identification data. The Onclicked event of the button executes an event dispatcher. The widget that created the ButtonWidget listens - has an event bound - to this dispatcher. You can actually bind to the button, you just need the button reference but I don’t see why you would do that because you can’t identify which button was clicked. The ButtonWidget can give such information.

My first thought is if you really need to create the building buttons at runtime? If you want a building menu like say Starcraft, then all the buttons could be created in the widget and set to be hidden, and then set to visible when appropriate.

I’ve been working a little bit on a project to see if I can figure it out myself, but it’s really just building on the link in your third post.

Basically I have a MainWidget containing the categories and buildings. When Creating the category widgets, they are given a unique ID. The Category widget has an event dispatcher that includes its ID that the Mainwidget listens for.
Then using a widget switcher the correct BuildingWidgets are shown. Widget switcher: HTF do I? Use the Widget Switcher in UMG - YouTube
So if the Category with the ID of 1 is clicked, the MainWidget hears that and updated the widget switcher so that the buildings of category 1 are shown.

The MainWidget also creates and listens to the BuildingButtons in the same manner.
The MainWidget is informed that a BuildingButton has been clicked and is provided with the ID, say building type. It then triggers an event dispatcher of its own, which I guess the player controller listens to and passes the ID. The Player controller can then use this ID to say spawn an actor or whatever.

I can share the project, it’s only 19MB. I used the PuzzleExample and the relevant blueprints are: PuzzlePlayerCharacter, MainWidget, BuildingWidget, Categorywidget. Most of it happens in the MainWidget.
https://www.dropbox.com/sh/06frsq45r…X2cF-vEDa?dl=0
Note that I am using some enums and hard coded TMaps to act as my ‘data base’.

Thanks a lot, I am going to take a look at your stuff!

It’s not strictly necessary to create the widget on runtime, but I thought it would be a good exercise, and it would make adding in buttons less of a hassle, since I would just give the new building the appropriate Type/Category/Sort-ID and add them to the Array.
If this gets me to get the hang of how to use some stuff, it’s a net win.

Dude, thanks a ton!
Seriously, you went above and beyond. I am in your debt.

Ironically, it seems I was doing everything right, but fell victim to a bug with the “Create Object From Class”-Node, where it does not update the inputs.
If you either delete the node and put in a new one, or select a different class, and then the right one again, it updates.

That missing Input, combined with the confusing naming in the original example really messed with my mind.

Anyway, it is working splendidly now.