简体   繁体   English

如何在虚幻引擎 4 C++ 中的小部件内通过单击的 UI 按钮访问索引?

[英]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.

相关问题 如何在虚幻引擎中将Do N节点从蓝图转换为C++? - How to convert the Do N node from blueprint to C++ in Unreal Engine? 如何从 Unreal Engine C++ 发送 GraphQL 请求? - How can I send a GraphQL request from Unreal Engine C++? 如何在虚幻引擎 4 中添加 SWidgetSwitcher 并获取/设置当前小部件索引? - How to add SWidgetSwitcher and get/set current widget index in Unreal Engine 4? 虚幻引擎/ C ++:如何根据屏幕上的2D UI窗口小部件绘制纹理遮罩 - Unreal Engine / C++: How to draw a texture mask based on where 2D UI Widgets are on-screen 如何在虚幻引擎中添加小部件 - How to add widget in Unreal Engine 如何创建一个界面,允许我访问使用 python 中的 MFC 实现的 C++ 中的按钮(和其他 ui)功能? - How do I create an interface that allows me to access button(and other ui) functions in C++ implemented using MFC from python? 在 C++/Unreal Engine 4 的不同类中访问静态变量 - Access static variable in a different class in C++/Unreal Engine 4 虚幻:如何使用 C++ 访问 MediaTexture 中的图像? - Unreal: How to access image in MediaTexture with C++? 我无法创建虚幻引擎 4 c++ 项目 - I am not able to create a unreal engine 4 c++ project 如何在虚幻引擎项目中使用C ++ Core Checker? - How to use C++ Core Checker in Unreal Engine project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM