Button Array with index sent to event?

Is it possible to create an array like this? For example in visual basic it was possible to create a button array, each had an Index property and raised the same event where Index was an input parameter, so that you could easily use the same logic for all of the buttons and know which button was pressed.

The closest I’ve been able to come up with is to make a custom function with an index input, and then have every button call it and manually pass the index as shown. It’s just messy and time consuming to do it this way, especially with 88 buttons.

Unreal Engine also has array variables that come with an index if that’s what you are asking?
You could probably make it an array of buttons.

I’m not sure what the best way to add 88 known buttons to the array would be, though. The “Make Array” node would work, but it would also make you plug all 88 buttons into it.

Array, no? The button does not carry an index and simply does not know where it is in the owning array. The most straightforward and flexible way is to make each button a widget so it can carry data:

  • you can add the buttons manually and assign indexes one by one, ofc.
  • or get all children of a panel and iterate → assign index

The above would create 51 buttons with ascending indexes, the parent (bottom image) will know the clicked one’s index. You can add additional data to the Event Dispatcher, like the reference of the pertinent button itself, if necessary.

Could you briefly describe what you’re making and how it’s supposed to work. I’m assuming you’re making a piano / keyboard, right? Each key is supposed to evoke an action of sort?

Sure. The project takes input from a MIDI keyboard and plays (re-mapped microtonal) notes based on the input. I will be selecting various pitches (say from a 22-tone octave, or a 5-tone octave) from a list and remapping them to various keys on the keyboard. I’d click a microtonal note to select it (stored in a “last clicked” var) and then click on whichever piano key(s) I want to assign to that specific note. I can create the logic to deal with which notes map to which keys, no problem, what I am looking for is a (lazy) way to call the same function from all 88 buttons, where the function knows which button was the instigator.

I already have the MIDI functionality working: I can play microtonal music through my PC’s speakers using a MIDI keyboard. I am at the point where I want to re-map each key to be any tone. (So that I can still have octaves repeat every 12th key, because you don’t use all of the possible pitches anyway.)

I know it sounds like something that is totally not what UE4 is made for, but I want to make this program and I want to learn UE4. I could make it in C# easily, but I wouldn’t learn anything. This sort of problem (and all the others I’ve had to work out) is exactly why it’s a useful exercise. :slight_smile:

Thanks, this sounds like it would work. I’m lost on how to set up the connections between these things though. I’d need an ELI5 to be able to set it up it. (Sorry, lifetime of structured programming, OOP and event-driven stuff is non-intuitive to me.)

what I am looking for is a (lazy) way
to call the same function from all 88
buttons, where the function knows
which button was the instigator

Sounds like you’ve done the heavy lifting already. You need 2 widgets, a Keyboard and a Key:

  • the key sends its own reference via an Event Dispatcher:

  • the keyboard creates the keys, adds them to a container (horizontal box) and registers the events:

That’s pretty much it. Pressing a key, fires the event in the keyboard:

Image from Gyazo

And you get whatever data from the key that was plugged into the Dispatcher.


This could be done without custom user widget wrapped buttons but if you need each button to contain data or be responsible for some logic, that’s the lazy way to do it.

I’ll just add that pretty much every actor, component, widget and their mother already comes with some native events. If you manually populated a container with some widgets, you can bind an existing element’s event like so:

So you could have the keyboard know that an interaction happened in one of its children:

This will work with a native button but, obviously, no data is being piped in here.

Could you upload the project above so I could look at all of the details? I get conceptually what each of those things is, I just am not getting how each of them connects to each other and I think I can work it out if I can dissect it.

Thanks either way for the time you’ve put into helping out. :slight_smile:

That’s 100% of the script in those 2 screenshots. There is nothing more apart from adding the main widget to the screen. There are no details here, sorry. That’s it.

Adding widget to the screen and showing cursor:

The keyboard has a horizontal box:

318426-keyboardagain.jpg

The key widget has just a button, nothing else.

Link:

But I feel you’ll be disappointed.

1 Like

That’s perfect. After dissecting it I get it now. Thank you. :slight_smile: Those events/dispatchers/bindings get me every time.