Hi there, happy Wednesday.
When my game server starts, it needs config data about players requested from a REST API. Without this config data, the game server’s best contribution is to terminate ASAP.
I can start the HTTP request in GameMode::InitGame()
but how do I block until the response arrives? I need this data for GameMode::PreLogin()
and GameMode::InitNewPlayer()
.
In JavaScript, I’d use async/await or promises. What’s the best way in UE4 and C++? Thank you in advance!
ADramaGameMode::ADramaGameMode()
{
//When the object is constructed, Get the HTTP module
Http = &FHttpModule::Get();
}
void ADramaGameMode::InitGame(const FString & MapName, const FString & Options, FString & ErrorMessage)
{
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &ADramaGameMode::OnResponseReceived);
//This is the url on which to process the request
Request->SetURL("http://localhost:8081/WebApi/getint.php");
Request->SetVerb("GET");
Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->ProcessRequest();
// How do I wait here until OnResponseReceived is bWasSuccessful?
}
void ADramaGameMode::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
// parse response
// unblock InitGame()
}
else
{
// retry or quit
}
}
Thank you!