Is it possible to wrap a delegate into a lambda?

Our delegates don’t currently have explicit support for lambdas, however, since a lambda that doesn’t capture anything will convert to a static function pointer, some lambdas will still work.

Your lambda doesn’t work since it’s capturing Event.

What you could do instead is not capture Event, but rather bind it to the delegate and take it as a parameter (this works for any kind of function bound to a delegate, not just lambdas).

HttpRequest->OnProcessRequestComplete().BindStatic([](FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded, FTheEventType Event)
{
int a = 100; // Just to see if this is gonna compile
}, Event);

Hi,

I have a little problem. I’m using HttpRequest in my analytics module. HttpRequest allows you to bind to a OnProcessRequestComplete delegate:

HttpRequest->OnProcessRequestComplete().BindRaw(this, &FAnalytcsWorker::OnRequestComplete);

Where the delegate is declared (IHttpRequest.h) as following:

DECLARE_DELEGATE_ThreeParams(FHttpRequestCompleteDelegate, FHttpRequestPtr, FHttpResponsePtr, bool);

However I need to pass an extra object (my analytics event itself) to the OnRequestComplete method (to handle a failed connection scenario) so I thought I could use a lambda expression and capture my event:

HttpRequest->OnProcessRequestComplete().BindRaw(this, [Event](FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
{
   int a = 100;  // Just to see if this is gonna compile
});

And of course this didn’t work (otherwise I wouldn’t ask this question)

error C2664: 'void TBaseDelegate_ThreeParams<void,FHttpRequestPtr,FHttpResponsePtr,bool>::BindRaw<AnalyticsModule::FAnalyticsWorker>(UserClass *,void (__cdecl AnalyticsModule::FAnalyticsWorker::* )(Param1Type,Param2Type,Param3Type) const)' : cannot convert argument 2 from 'AnalyticsModule::FAnalyticsWorker::Run::<lambda_b1948087e09663f82741f80176a06865>' to 'void (__cdecl AnalyticsModule::FAnalyticsWorker::* )(Param1Type,Param2Type,Param3Type)'

So I guess wrapping a delegate into a lambda is not the same as wrapping a function into a lambda. But maybe there is a way? Any ideas?

Ah. Sorry, that should have been BindStatic rather than BindRaw.

BindRaw binds a member function, BindStatic binds either a static or non-member function (including a lambda that will convert to a static function).

I’ve updated my answer above.

Actually I tried to compile my lambda without capturing Event and it didn’t work either (same error message). But you’re absolutely right - I can just pass payload while binding my delegate! It solves my problem - thank you, Jamie!

FCoreDelegates::OnMountPak.Execute()
Who can explain,What is the role of this function is?Thanks!