简体   繁体   English

如何使用 UE4 C++ 中的参数将 UFunction 添加到接口的委托?

[英]How to add a UFunction to an interface's delegate with parameters in UE4 C++?

I am looking use web sockets in Unreal.我正在寻找在虚幻中使用 web sockets 。 I am following the tutorial found here: Web Socket Tutorial我正在关注这里的教程: Web Socket Tutorial

Most notably I am trying to bind to the events before connection.最值得注意的是,我试图在连接之前绑定到事件。 In the example, they use .AddLambda however, I would like to try to use .AddUFunction .在示例中,他们使用.AddLambda但是,我想尝试使用.AddUFunction It seems the function takes in the Object, the function name, and VarTypes...types .似乎 function 采用了 Object、 function 名称和VarTypes...types I can't seem to figure out what the last parameter is for the delegates that use parameters.我似乎无法弄清楚使用参数的代表的最后一个参数是什么。 (At least I believe that is the problem anyway) The functions themselves have the correct signature and matches the delegates I want to bind to. (至少我相信这是问题所在)函数本身具有正确的签名并匹配我想要绑定的委托。

Here is what I have so far:这是我到目前为止所拥有的:

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();

}

It gives me the following error messages:它给了我以下错误消息:

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

The function definitions: function 定义:

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)
{
}

I have never come across this .AddUFunction before and I can't seem to find any examples of how to use it.我以前从未遇到过这个.AddUFunction ,我似乎找不到任何如何使用它的例子。 If anyone can help me out or point me in the right direction, it would be appreciated.如果有人可以帮助我或指出我正确的方向,将不胜感激。

Please read the Documentation on Events请阅读有关事件的文档

You must declare function signatures that match that of the Event Delegate within your class for which a function pointer will be bound.您必须声明 function 签名与 class 中的事件委托的签名匹配,function 指针将被绑定。

在此处输入图像描述

The example image above is from the Engine Source.上面的示例图像来自引擎源。

Works for me!为我工作!

SandboxGameInstance.cpp沙盒游戏实例.cpp

#include "SandboxGameInstance.h"

#include "WebSocketsModule.h"
#include "IWebSocket.h"       // Socket definition
void USandboxGameInstance::Init()
{
    Super::Init();
    FWebSocketsModule& Module = FModuleManager::LoadModuleChecked<FWebSocketsModule>(TEXT("WebSockets"));

    const FString ServerURL = TEXT("ws://127.0.0.1:3000/"); // Your server URL. You can use ws, wss or wss+insecure.
    const FString ServerProtocol = TEXT("ws");              // The WebServer protocol you want to use.

    TSharedPtr<IWebSocket> 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();
}

void USandboxGameInstance::OnSocketConnection()
{
    UE_LOG(LogTemp, Warning, TEXT("CONNECTED"));
}

void USandboxGameInstance::OnSocketConnectionError(const FString& ErrorMessage)
{
    UE_LOG(LogTemp, Warning, TEXT("ERROR"));
}

void USandboxGameInstance::OnSocketClosed(int32 StatusCode, const FString& Reason, bool WasClean)
{
    UE_LOG(LogTemp, Warning, TEXT("CLOSED"));
}

void USandboxGameInstance::OnSocketReceiveMessage(const FString& Message)
{
    UE_LOG(LogTemp, Warning, TEXT("RECEIVED"));
}

void USandboxGameInstance::OnSocketSentMessage(const FString& Message)
{
    UE_LOG(LogTemp, Warning, TEXT("SENT"));
}

SandboxGameInstance.h沙盒游戏实例.h

#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "SandboxGameInstance.generated.h"

UCLASS()
class SANDBOX_API USandboxGameInstance : public UGameInstance
{
    GENERATED_BODY()
public:
    virtual void Init() override;

    UFUNCTION()
    void OnSocketConnection();

    UFUNCTION()
    void OnSocketConnectionError(const FString& ErrorMessage);

    UFUNCTION()
    void OnSocketClosed(int32 StatusCode, const FString& Reason, bool WasClean);

    UFUNCTION()
    void OnSocketReceiveMessage(const FString& Message);

    UFUNCTION()
    void OnSocketSentMessage(const FString& Message);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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