Capturing index in For Loop when creating buttons

Hi everyone,

What I want

I’m trying to create a set of radio buttons dynamically using a For Loop in Blueprints. Each button should have a unique index, and when clicked, it should print the correct index.

I expected it to work similarly to Kotlin’s closure behavior, like this example in Jetpack Compose:

for (i in 0..2) {
    Button(onClick = { print("User clicked Button $i") })
}

What I’ve tried

I’m pretty happy with “Add Text Button”. I can bind event to OnClick event.

Now I’m trying to create three buttons, and they should print “Button $index” when clicked.

When tested, all three buttons print the last index 3. Because

  1. All buttons are binded to same event.
  2. When event is fired, event refers to index of for loop node, which happens to be 3.

Question

How can I correctly capture the index for each button so that it prints the right number when clicked?
If blueprint is not capable of this requirements, please help me with C++ way.

What I want:

  • I don’t want to change behavior of TextButton. I want to have TextButton decoupled from radio button thing. So TextButton widget should not store index inside, or pass reference to parent widget.
  • Be flexible with number of buttons. So creating three unique events for three buttons may not work.

Thanks in advance!