- Newest
- Most votes
- Most comments
It was an issue with FProcessParameters
definition. Added static
keyword and issue is resolved.
Here is the slightly modified and fixed code which is taken from the GameLift documentation here. Hope it helps someone having the same issue.
void A<omitted>GameMode::InitGameLift()
{
UE_LOG(GameServerLog, Log, TEXT("Initializing the GameLift Server"));
// Getting the module first.
FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK"));
// Define the server parameters
FServerParameters serverParameters;
// AuthToken returned from the "aws gamelift get-compute-auth-token" API. Note this will expire and require a new call to the API after 15 minutes.
if (FParse::Value(FCommandLine::Get(), TEXT("-authtoken="), serverParameters.m_authToken))
{
UE_LOG(GameServerLog, Log, TEXT("AUTH_TOKEN: %s"), *serverParameters.m_authToken)
}
// The Host/Compute ID of the GameLift Anywhere instance.
if (FParse::Value(FCommandLine::Get(), TEXT("-hostid="), serverParameters.m_hostId))
{
UE_LOG(GameServerLog, Log, TEXT("HOST_ID: %s"), *serverParameters.m_hostId)
}
// The EC2 or Anywhere Fleet ID.
if (FParse::Value(FCommandLine::Get(), TEXT("-fleetid="), serverParameters.m_fleetId))
{
UE_LOG(GameServerLog, Log, TEXT("FLEET_ID: %s"), *serverParameters.m_fleetId)
}
// The WebSocket URL (GameLiftServiceSdkEndpoint).
if (FParse::Value(FCommandLine::Get(), TEXT("-websocketurl="), serverParameters.m_webSocketUrl))
{
UE_LOG(GameServerLog, Log, TEXT("WEBSOCKET_URL: %s"), *serverParameters.m_webSocketUrl)
}
// The PID of the running process
serverParameters.m_processId = FString::Printf(TEXT("%d"), GetCurrentProcessId());
UE_LOG(GameServerLog, Log, TEXT("PID: %s"), *serverParameters.m_processId);
// InitSDK will establish a local connection with GameLift's agent to enable further communication.
FGameLiftGenericOutcome initSdkOutcome = gameLiftSdkModule->InitSDK(serverParameters);
if (initSdkOutcome.IsSuccess())
{
UE_LOG(GameServerLog, Log, TEXT("GameLift InitSDK succeeded"));
}
else
{
UE_LOG(GameServerLog, Log, TEXT("ERROR: InitSDK failed"));
FGameLiftError gameLiftError = initSdkOutcome.GetError();
UE_LOG(GameServerLog, Log, TEXT("ERROR: %s"), *gameLiftError.m_errorMessage);
return;
}
// When a game session is created, GameLift sends an activation request to the game server and passes along the game session object containing game properties and other settings.
// Here is where a game server should take action based on the game session object.
// Once the game server is ready to receive incoming player connections, it should invoke GameLiftServerAPI.ActivateGameSession()
auto onGameSession = [=](Aws::GameLift::Server::Model::GameSession gameSession)
{
FString gameSessionId = FString(gameSession.GetGameSessionId());
UE_LOG(GameServerLog, Log, TEXT("GameSession Initializing: %s"), *gameSessionId);
gameLiftSdkModule->ActivateGameSession();
};
static FProcessParameters params;
params.OnStartGameSession.BindLambda(onGameSession);
// OnProcessTerminate callback. GameLift will invoke this callback before shutting down an instance hosting this game server.
// It gives this game server a chance to save its state, communicate with services, etc., before being shut down.
// In this case, we simply tell GameLift we are indeed going to shutdown.
params.OnTerminate.BindLambda([=]()
{
UE_LOG(GameServerLog, Log, TEXT("Game Server Process is terminating"));
gameLiftSdkModule->ProcessEnding();
});
// This is the HealthCheck callback.
// GameLift will invoke this callback every 60 seconds or so.
// Here, a game server might want to check the health of dependencies and such.
// Simply return true if healthy, false otherwise.
// The game server has 60 seconds to respond with its health status. GameLift will default to 'false' if the game server doesn't respond in time.
// In this case, we're always healthy!
params.OnHealthCheck.BindLambda([]()
{
UE_LOG(GameServerLog, Log, TEXT("Performing Health Check"));
return true;
});
// This game server tells GameLift that it will listen on port 7777 for incoming player connections.
params.port = 7777;
// Here, the game server tells GameLift what set of files to upload when the game session ends.
// GameLift will upload everything specified here for the developers to fetch later.
TArray<FString> logfiles;
logfiles.Add(TEXT("C:\\game\\<omitted>\\Saved\\Logs"));
params.logParameters = logfiles;
// Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
UE_LOG(GameServerLog, Log, TEXT("Calling Process Ready"));
FGameLiftGenericOutcome processReadyOutcome = gameLiftSdkModule->ProcessReady(params);
if (processReadyOutcome.IsSuccess())
{
UE_LOG(GameServerLog, Log, TEXT("Process Ready Succeded"));
}
else
{
UE_LOG(GameServerLog, Log, TEXT("ERROR: Process Ready Failed"));
FGameLiftError processReadyError = processReadyOutcome.GetError();
UE_LOG(GameServerLog, Log, TEXT("ERROR: %s"), *processReadyError.m_errorMessage);
}
UE_LOG(GameServerLog, Log, TEXT("Init GameLift complete"));
}
Thanks.
Merih Dereli
Couple of comments on your solution. Example from GameLift documentation is just to make you started, so do not consider it as best practice. In my opinion calls to GameLift SDK should not be made from GameMode, but you should create class YourProjectGameLiftSubsystem that is derived from UGameInstanceSubsystem with private member FProcessParameters GameLiftProcessParams; Also use of Lambda functions for OnHealthCheck, OnTerminate and onGameSession is ok if you are just doing some logging but it would be better to create regular functions and then bind them to those delegates e.g.
GameLiftProcessParams.OnStartGameSession.BindUObject(this, &UYourProjectGameLiftSubsystem::OnGameSessionActivated);
void UYourProjectGameLiftSubsystem::OnGameSessionActivated(Aws::GameLift::Server::Model::GameSession ActivatedSession) { ... }
Relevant content
- Accepted Answerasked 4 years ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 5 months ago
Hey! Thanks a lot for your comments. Will definitely consider them.