Search code examples
c++unreal-engine4

Unreal Engine 4: dynamic multicast delegate binding


at the moment i am playing around with unreal engine's HTTP module and wrote a class to query weather information from a web service. First I created a general HTTP service class like this:

#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);
};

I declared a dynamic multicast delegate for the OnResponseReceived event. This method broadcasts the received JSON string:

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

}

After that I created a subclass of the HTTPService class for a specific web 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);

};

Now I want to use this for a custom actor, which I wrote. In the details panel customization I created a custom row to hold a button like this:

//....
        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)
            ];
//...

When you click the button, it fires the function to query the web api. What I want to do now is to bind a function to the dynamic multicast delegate. I want to fire a method of my custom actor when the response was received. How would you do that? What would be a good program structure? Can anybody provide a piece of code as reference?

​​​​​​​Thanks in advance =)


Solution

  • I managed to make it work with a DECLARE_MULTICAST_DELEGATE_OneParaminstead of a DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam.