Download

Bind two delegates

Hi,

I am using the HttpRequest class. On this page it says delegates can be bound to other delegates using the Bind method, but this method is not defined in DelegateSignatureImpl.inl.


TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(FString::Printf(TEXT("http://%s%s?key="), host, uri, apikey));
HttpRequest->OnProcessRequestComplete().Bind(complete); // << Bind is undefined
HttpRequest->ProcessRequest();

What is the correct way to bind one delegate to another?

I know I could return the delegate and have the caller bind it, but then in cases like the above this can result in a race condition between registration and the callback firing.

You can see an example of how to use it in the source code:


FHttpModule& HttpModule = FModuleManager::LoadModuleChecked<FHttpModule>("HTTP");
FHttpRequestRef HttpRequest = HttpModule.Get().CreateRequest();

**HttpRequest->OnProcessRequestComplete().BindRaw(this, &FOneSkyListProjectGroupsWorker::Query_HttpRequestComplete);**
HttpRequest->SetURL(Url);
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
HttpRequest->SetVerb(TEXT("GET"));
HttpRequest->ProcessRequest();

**Query_HttpRequestComplete **signature:


void Query_HttpRequestComplete(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded);

Thanks SolidSk, but that looks to me like its binding the function Query_HttpRequestComplete to the delegate.

What I meant is, declare an FHttpRequestCompleteDelegate, bind a function to it somewhere else, then pass the bound delegate instance and “connect up” this and the one returned from OnProcessRequestComplete().

e.g.


class1::func1()
{
    FHttpRequestCompleteDelegate existingDelegate;
    existingDelegate.BindRaw(this, &class1:callback);
    class2instance->func2(existingDelegate);
}

class2:func2(FHttpRequestCompleteDelegate& existingDelegate)
{
**    HttpRequest->OnProcessRequestComplete().Bind(**existingDelegate**);**
    HttpRequest->SetURL(Url);
    HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
    ...
}