[英]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++.我正在使用 Unreal Engine 4 和 C++ 制作 UI 菜单。 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.代码有点简化,我实际上是在一个循环中动态创建这个按钮,所以我最终得到了很多按钮,所有这些按钮都调用相同的 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?
有没有办法“知道”按下了哪个按钮,例如,如果我按下第一个按钮,我得到 1,如果我按下第二个按钮,我得到 2,等等?
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?因此,您可以做的是制作自己的按钮 class ,您可以动态创建它,然后单击返回某种形式的标识符,例如和索引之类的? 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.
如果您想保持通用性,您还可以将它们添加到某种容器/列表中,并通过返回数组的容器上的 GetAllChildren 访问特定按钮。
Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.