简体   繁体   中英

How do I access the index from a clicked UI Button inside a widget in Unreal Engine 4 C++?

I am working on a UI menu using Unreal Engine 4 and C++. I have this code (taken from this thread):

H: 
UPROPERTY(meta = (BindWidget)) UButton* TestButton;

UFUNCTION() void OnClick();

CPP:
void UWidgetClassName::NativeConstruct() 
{
    Super::NativeConstruct();

    if (!TestButton->OnClicked.IsBound()) TestButton->OnClicked.AddDynamic(this, &UWidgetClassName::OnClick);
}

void UWidgetClassName::OnClick() 
{
     //I want to access the index of the clicked button here
}

The code is a bit simplified, I actually create this buttons dynamically inside a loop, so I end up with many buttons, all of which call the same function. Is there a way to "know" which button was pressed, so for example if I press the first button, I get 1, if I press the second, I get 2, etc?

Thanks a lot:)

So what you could do is make your own button class which you create dynamically and on click you return some form of identifier like and index or something? If you want to keep it generic you can also add them to some sort of container/list and access the specific button via GetAllChildren on the container which returns an array.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM