首页 > 解决方案 > 如何在 UE4 C++ 中使用参数将 UFunction 添加到接口的委托?

问题描述

我正在寻找在虚幻中使用网络套接字。我正在关注此处找到的教程:Web Socket Tutorial

最值得注意的是,我试图在连接之前绑定到事件。在示例中,他们使用.AddLambda但是,我想尝试使用.AddUFunction. 该函数似乎接受了对象、函数名和VarTypes ...types. 我似乎无法弄清楚使用参数的代表的最后一个参数是什么。(至少我相信这是问题所在)函数本身具有正确的签名并匹配我想要绑定的委托。

这是我到目前为止所拥有的:

void AWebSocketController::CreateWebSocket(FString ServerUrl, FString ServerProtocol)
{
  Socket = FWebSocketsModule::Get().CreateWebSocket(ServerUrl, ServerProtocol);

  // We bind to the events
  Socket->OnConnected().AddUFunction(this, FName("OnSocketConnection"));

  Socket->OnConnectionError().AddUFunction(this, FName("OnSocketConnectionError"));

  Socket->OnClosed().AddUFunction(this, FName("OnSocketClosed"));

  Socket->OnMessage().AddUFunction(this, FName("OnSocketReceiveMessage"));

  Socket->OnMessageSent().AddUFunction(this, FName("OnSocketSentMessage"));

  // And we finally connect to the server. 
  Socket->Connect();

}

它给了我以下错误消息:

error LNK2005: "public: void __cdecl AWebSocketController::OnSocketClosed(int,class FString const &,bool)" (?OnSocketClosed@AWebSocketController@@QEAAXHAEBVFString@@_N@Z) already defined in WebSocketController.cpp.obj
error LNK2005: "public: void __cdecl AWebSocketController::OnSocketConnection(void)" (?OnSocketConnection@AWebSocketController@@QEAAXXZ) already defined in WebSocketController.cpp.obj
error LNK2005: "public: void __cdecl AWebSocketController::OnSocketConnectionError(class FString const &)" (?OnSocketConnectionError@AWebSocketController@@QEAAXAEBVFString@@@Z) already defined in WebSocketController.cpp.obj
error LNK2005: "public: void __cdecl AWebSocketController::OnSocketReceiveMessage(class FString const &)" (?OnSocketReceiveMessage@AWebSocketController@@QEAAXAEBVFString@@@Z) already defined in WebSocketController.cpp.obj
error LNK2005: "public: void __cdecl AWebSocketController::OnSocketSentMessage(class FString const &)" (?OnSocketSentMessage@AWebSocketController@@QEAAXAEBVFString@@@Z) already defined in WebSocketController.cpp.obj

函数定义:

void AWebSocketController::OnSocketConnection()
{
}

void AWebSocketController::OnSocketConnectionError(const FString& ErrorMessage)
{
}

void AWebSocketController::OnSocketClosed(int32 StatusCode, const FString& Reason, bool WasClean)
{
}

void AWebSocketController::OnSocketReceiveMessage(const FString& Message)
{
}

void AWebSocketController::OnSocketSentMessage(const FString& Message)
{
}

我以前从未遇到过这种情况.AddUFunction,而且我似乎找不到任何如何使用它的示例。如果有人可以帮助我或指出我正确的方向,将不胜感激。

标签: c++unreal-engine4

解决方案


请阅读有关事件的文档

您必须声明与您的类中的事件委托相匹配的函数签名,函数指针将被绑定。

在此处输入图像描述

上面的示例图像来自引擎源。


推荐阅读