How to change/return variable in lambda?

i am using HTTP to make response, and here is my code:

TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
	FHttpRequestCompleteDelegate& Delegate = HttpRequest->OnProcessRequestComplete();

	Delegate.BindLambda([](FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded){
		if (bSucceeded)
			FString result = HttpResponse->GetContentAsString();
			result = "[" + result + "]";

			TSharedRef<TJsonReader<>> t_reader = TJsonReaderFactory<>::Create(result);
			TArray<TSharedPtr<FJsonValue>> JsonParsed;
			bool BFlag = FJsonSerializer::Deserialize(t_reader, JsonParsed);
			if (BFlag)
				TArray<TSharedPtr<FJsonValue>> SuccessOrFailed = JsonParsed[0]->AsObject()->GetArrayField("ret");
				FString successStr = SuccessOrFailed[0]->AsString();

				if (successStr.StartsWith("SUCCESS")) {
					int32 tempstamp = JsonParsed[0]->AsObject()->GetObjectField("data")->GetIntegerField("t");//THE DATA I NEED TO RETURN
					FString TmpString = "data :" + tempstamp;
					GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TmpString);

	HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/x-www-form-urlencoded"));

It needs to be bind to the lambda, but in this lambda i cant change any variable outside. So i am confused about how to change variable outside or send this int32 to blueprint. Maybe my writing of HTTP request is not correct? thanks for everyone’s answer!

You can do by-reference capture using this syntax: [&, ...]. It’ll let you access this inside lambda.

For example:

Delegate.BindLambda([&](FHttpRequestPtr req, FHttpResponsePtr res, bool bSucceeded){
  // Do something
  this->MyStamp = tempStamp;

Reference: Lambda expressions (since C++11) -

1 Like

Hello, I wonder if you forgot to accept and upvote the answer?

i feel sorry for my delayed upvote!