Download

Wait till function is finished

Hello I’m making a connection to my SQL database with PHP.
I can succesfully get all my data in UE4, but I need a timer to wait till I have all my data and then continue.

How can I pause my code after
Request->OnProcessRequestComplete().BindUObject(this, &ADataConnection::CompletedHTTPRequest);
Untill it finishes CompletedHTTPRequest?



void ADataConnection::ConnectToDatabase(ConnectDatabaseEnum& Branches)
{
	FString JsonString;
	TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<TCHAR>::Create(&JsonString);

	JsonWriter->WriteObjectStart();
	JsonWriter->WriteValue(TEXT("sqlSelect"), sqlSelectQuery);
	JsonWriter->WriteObjectEnd();
	JsonWriter->Close();

	TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
	Request->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
	Request->SetURL("myURL");
	Request->SetVerb("POST");
	Request->SetContentAsString(JsonString);

	Request->OnProcessRequestComplete().BindUObject(this, &ADataConnection::CompletedHTTPRequest);

	if (Request->ProcessRequest())
	{

		//	UE_LOG(MyLog, Warning, TEXT("ttt %s"), *JsonString);
	}
	else
	{

		//	UE_LOG(MyLog, Warning, TEXT("Request Failed"));
	}

	if (dataInt == 1)
	{
		Branches = ConnectDatabaseEnum::Succes;
		dataInt = 0;
	}
	else
	{
		Branches = ConnectDatabaseEnum::Failed;
	}
}

void ADataConnection::CompletedHTTPRequest(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
{
	if (!response.IsValid())
	{
		UE_LOG(MyLog, Warning, TEXT("invalid response"));
	}
	else if (EHttpResponseCodes::IsOk(response->GetResponseCode()))
	{
		FString msg = response->GetContentAsString();


		UE_LOG(MyLog, Warning, TEXT("%s"), *msg);
		

		TSharedPtr<FJsonObject> JsonParsed = MakeShareable(new FJsonObject());
		TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(*msg);
		if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
		{
			size = JsonParsed->GetStringField(TEXT("Size"));
			tower = JsonParsed->GetStringField(TEXT("Tower"));
			floor = JsonParsed->GetStringField(TEXT("FloorNumber"));
			availability = JsonParsed->GetStringField(TEXT("Availabilty"));
			availabilityDate = JsonParsed->GetStringField(TEXT("AvailabiltyDate"));
			floorDescription = JsonParsed->GetStringField(TEXT("FloorDescription"));
			imagePath = JsonParsed->GetStringField(TEXT("ImagePath"));
			iD = JsonParsed->GetStringField(TEXT("ID"));

			sizeA.Add(size);
			towerA.Add(tower);
			floorA.Add(floor);
			availabilityA.Add(availability);
			imagePaths.Add(imagePath);
			iDA.Add(iD);
			availabilityDateA.Add(availabilityDate);
			floorDescriptionA.Add(floorDescription);

			dataInt = 1;
		}
		else
		{
			dataInt = 0;
			UE_LOG(MyLog, Warning, TEXT("No JsonString"));
		}
	}
	else
	{
		UE_LOG(MyLog, Warning, TEXT("request failed"));
	}
}


Well you could make “Request” available for the whole class and just call another function at the end of “CompletedHTTPRequest” that then uses the variable again.

The thing is that I want to use only the function ConnectToDatabase because I made it useable in blueprints.
So that way I only need 1 function in the bluepritns. But when I execute the connecttodatabase it doesn’t wait for completedHTTPRequest like the function .wait() in c++.
And I was just thinking maybe calling a custom event at the end instead, but that’s something i will try out soon.

Still not working

I got the first time working now with a custom event.
But my array gets 4 times the same value instead of 4 diffrent.

UE_LOG(MyLog, Warning, TEXT("%s"), *imagePath);
gives 4 diffrent values but
imagePaths.Add(imagePath);
gets 4 times the same values why?
I also tried to Add(*imagePath); but no succes.



void ADataConnection::CompletedHTTPRequest(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
{
	if (!response.IsValid())
	{
		UE_LOG(MyLog, Warning, TEXT("invalid response"));
	}
	else if (EHttpResponseCodes::IsOk(response->GetResponseCode()))
	{
		FString msg = response->GetContentAsString();


	//	UE_LOG(MyLog, Warning, TEXT("%s"), *msg);
		

		TSharedPtr<FJsonObject> JsonParsed = MakeShareable(new FJsonObject());
		TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(*msg);
		if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
		{
			size = JsonParsed->GetStringField(TEXT("Size"));
			tower = JsonParsed->GetStringField(TEXT("Tower"));
			floor = JsonParsed->GetStringField(TEXT("FloorNumber"));
			availability = JsonParsed->GetStringField(TEXT("Availabilty"));
			availabilityDate = JsonParsed->GetStringField(TEXT("AvailabiltyDate"));
			floorDescription = JsonParsed->GetStringField(TEXT("FloorDescription"));
			imagePath = JsonParsed->GetStringField(TEXT("ImagePath"));
			iD = JsonParsed->GetStringField(TEXT("ID"));

			UE_LOG(MyLog, Warning, TEXT("%s"), *imagePath);

			sizeA.Add(size);
			towerA.Add(tower);
			floorA.Add(floor);
			availabilityA.Add(availability);
			imagePaths.Add(imagePath);
			iDA.Add(iD);
			availabilityDateA.Add(availabilityDate);
			floorDescriptionA.Add(floorDescription);

			
			CustomEventData();
		}
		else
		{
			UE_LOG(MyLog, Warning, TEXT("No JsonString"));
		}
	}
	else
	{
		UE_LOG(MyLog, Warning, TEXT("request failed"));
	}
}


I know this is old, but I’m going to comment because I was searching for a similar answer and came across this thread.

I think the method you are looking for in this case is the FHttpManager::Flush(bool). After doing a ProcessRequest() call the Flush method and it will wait until all the requests have been finished processing including the binded callbacks OnProcessRequestComplete. This is ideal for minimum amount of requests made.