Unreal Engine4:动态多播委托绑定
Unreal Engine 4: dynamic multicast delegate binding
目前我正在使用 unreal engine 的 HTTP 模块并编写了一个 class 来从 Web 服务查询天气信息。
首先,我创建了一个通用的 HTTP 服务 class,如下所示:
#pragma once
#include "CoreMinimal.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "Json.h"
#include "HTTPService.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FHTTPOnResponseRecievedDelegate, const FString&, HttpData);
UCLASS()
class THMVRAYPLUGIN_API UHTTPService : public UClass
{
GENERATED_BODY()
private:
FHttpModule * Http;
FString AuthorizationHeader = "Authorization";
void SetAuthorizationHash(FString Hash, TSharedRef<IHttpRequest>& Request);
void SetRequestHeaders(TSharedRef<IHttpRequest>& Request);
bool ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful);
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccesful);
void Send(TSharedRef<IHttpRequest>& Request);
public:
UHTTPService();
FHTTPOnResponseRecievedDelegate responseReceived;
TSharedRef<IHttpRequest> GetRequest(FString request);
};
我为 OnResponseReceived 事件声明了一个动态多播委托。
此方法广播收到的 JSON 字符串:
void UHTTPService::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccesful)
{
if (ResponseIsValid(Response, bWasSuccesful)) {
FString JSONString = Response->GetContentAsString();
UE_LOG(LogTemp, Warning, TEXT("Http Response: %s"), *JSONString);
responseReceived.Broadcast(JSONString);
}
}
之后我为特定网站创建了 HTTPService class 的子class API:
#pragma once
#include "CoreMinimal.h"
#include "HTTPService.h"
#include "HTTPWeatherService.generated.h"
UCLASS()
class THMVRAYPLUGIN_API UHTTPWeatherService : public UHTTPService
{
GENERATED_BODY()
private:
FString APIBaseURL = "xxxx";
FString APIKey = "xxxx";
public:
UHTTPWeatherService();
void queryWeatherAPIForCoords(float lat, float lon);
};
现在我想将其用于我编写的自定义演员。
在详细信息面板自定义中,我创建了一个自定义行来放置这样的按钮:
//....
auto OnWeatherButtonClicked = [myActor]
{
float lat = myActor->latitude;
float lon = myActor->longitude;
UHTTPWeatherService * weatherService = NewObject<UHTTPWeatherService>();
weatherService->queryWeatherAPIForCoords(lat, lon);
return FReply::Handled();
};
experimentalGroup.AddWidgetRow()
.ValueContent()
[
SNew(SButton)
.Text(LOCTEXT("Current weather", "Get weather at current location"))
.ToolTipText(LOCTEXT("Uses a web api to get information on the current weather at the current coords","Uses a web api to get information on the current weather at the current coords"))
.OnClicked_Lambda(OnWeatherButtonClicked)
];
//...
当您单击该按钮时,它会触发查询网络的功能 api。
我现在要做的是将一个函数绑定到动态多播委托。
我想在收到响应时触发我的自定义演员的方法。
你会怎么做?什么是好的程序结构?
谁能提供一段代码作为参考?
提前致谢=)
我设法让它与 DECLARE_MULTICAST_DELEGATE_OneParam
而不是 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam
一起工作。
目前我正在使用 unreal engine 的 HTTP 模块并编写了一个 class 来从 Web 服务查询天气信息。 首先,我创建了一个通用的 HTTP 服务 class,如下所示:
#pragma once
#include "CoreMinimal.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "Json.h"
#include "HTTPService.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FHTTPOnResponseRecievedDelegate, const FString&, HttpData);
UCLASS()
class THMVRAYPLUGIN_API UHTTPService : public UClass
{
GENERATED_BODY()
private:
FHttpModule * Http;
FString AuthorizationHeader = "Authorization";
void SetAuthorizationHash(FString Hash, TSharedRef<IHttpRequest>& Request);
void SetRequestHeaders(TSharedRef<IHttpRequest>& Request);
bool ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful);
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccesful);
void Send(TSharedRef<IHttpRequest>& Request);
public:
UHTTPService();
FHTTPOnResponseRecievedDelegate responseReceived;
TSharedRef<IHttpRequest> GetRequest(FString request);
};
我为 OnResponseReceived 事件声明了一个动态多播委托。 此方法广播收到的 JSON 字符串:
void UHTTPService::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccesful)
{
if (ResponseIsValid(Response, bWasSuccesful)) {
FString JSONString = Response->GetContentAsString();
UE_LOG(LogTemp, Warning, TEXT("Http Response: %s"), *JSONString);
responseReceived.Broadcast(JSONString);
}
}
之后我为特定网站创建了 HTTPService class 的子class API:
#pragma once
#include "CoreMinimal.h"
#include "HTTPService.h"
#include "HTTPWeatherService.generated.h"
UCLASS()
class THMVRAYPLUGIN_API UHTTPWeatherService : public UHTTPService
{
GENERATED_BODY()
private:
FString APIBaseURL = "xxxx";
FString APIKey = "xxxx";
public:
UHTTPWeatherService();
void queryWeatherAPIForCoords(float lat, float lon);
};
现在我想将其用于我编写的自定义演员。 在详细信息面板自定义中,我创建了一个自定义行来放置这样的按钮:
//....
auto OnWeatherButtonClicked = [myActor]
{
float lat = myActor->latitude;
float lon = myActor->longitude;
UHTTPWeatherService * weatherService = NewObject<UHTTPWeatherService>();
weatherService->queryWeatherAPIForCoords(lat, lon);
return FReply::Handled();
};
experimentalGroup.AddWidgetRow()
.ValueContent()
[
SNew(SButton)
.Text(LOCTEXT("Current weather", "Get weather at current location"))
.ToolTipText(LOCTEXT("Uses a web api to get information on the current weather at the current coords","Uses a web api to get information on the current weather at the current coords"))
.OnClicked_Lambda(OnWeatherButtonClicked)
];
//...
当您单击该按钮时,它会触发查询网络的功能 api。 我现在要做的是将一个函数绑定到动态多播委托。 我想在收到响应时触发我的自定义演员的方法。 你会怎么做?什么是好的程序结构? 谁能提供一段代码作为参考?
提前致谢=)
我设法让它与 DECLARE_MULTICAST_DELEGATE_OneParam
而不是 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam
一起工作。