In this How-To we'll look at how to start and maintaing a Server Session in Unreal Engine, using the Unreal Server SDK in C++.
Prerequisites
- A LootLocker account
- A created game in the web console
- An Unreal Server Project
- LootLocker Unreal Server SDK installed and set up in your project.
A Server session is valid for 60 minutes, any subsequent calls to LootLocker will set the time again to 60 minutes. To make sure that our Server always is available, we will call LootLocker every 55 minutes to make sure that we always have a fresh server session token to use.
#include "ServerSession.h"#include "LootLockerServerForCpp.h"#include "Templates/SharedPointer.h"#include "TimerManager.h"
// Timer handle to maintain the sessionFTimerHandle MaintainSessionTimerHandle;
void OnServerAuthCompleted(FLootLockerServerAuthenticationResponse Response){ if (Response.Success) { UE_LOG(LogTemp, Log, TEXT("LootLocker server session started successfully. Next refresh in 55 minutes.")); // Here we create a new instance of ServerSession to refresh the session. // We recommend using a singleton pattern or a static instance for better management in a real application. // Adapt to your game code as needed. TSharedPtr<ServerSession> MyServerSession = MakeShared<ServerSession>(); if (GWorld) { FTimerDelegate TimerDel = FTimerDelegate::CreateRaw(MyServerSession, &ServerSession::MaintainServerSession); GWorld->GetTimerManager().SetTimer( MyServerSession->MaintainSessionTimerHandle, TimerDel, 3300.0f, // 3300 seconds = 55 minutes true // Loop the timer to maintain the session every 55 minutes ); } } else { UE_LOG(LogTemp, Error, TEXT("Failed to start LootLocker session: %s"), *Response.ErrorData.Message); }}
// Call this function when you start your Server: GameModeBase, GameInstance, Server Actor, etc.void ServerSession::StartLootLockerSession(){ // CPP delegate FLootLockerServerAuthResponseDelegate CPPDelegate; CPPDelegate.BindStatic(&OnServerAuthCompleted);
// Call Start Session ULootLockerServerForCpp::StartSession(CPPDelegate);}
void OnMaintainSessionCompleted(FLootLockerServerMaintainSessionResponse Response){ if (Response.Success) { UE_LOG(LogTemp, Log, TEXT("LootLocker server session refreshed. Next refresh in 55 minutes.")); } else { UE_LOG(LogTemp, Error, TEXT("Failed to start refresh session: %s"), *Response.ErrorData.Message); }}
void ServerSession::MaintainServerSession(){ // CPP delegate FLootLockerServerMaintainSessionResponseDelegate CPPDelegate; CPPDelegate.BindStatic(&OnMaintainSessionCompleted);
// Call Start Session ULootLockerServerForCpp::MaintainSession(CPPDelegate);}We start a server session and then set a delay for every 3300 seconds (55 minutes) after which we call maintain session so as to not have the token expire while the server is running.
In the above code, we started a server session with LootLocker and set a looping timer to maintain the session every 55 minutes. Now we can be sure that our server always is ready whenever we need to talk with LootLocker.
Conclusion
In this How-to we’ve started a LootLocker Session and made sure that it stays alive as long as our Server is running. Now that you have a LootLocker Server Session, you can start using any of the features available in the Unreal Server SDK. You can use the Unreal Engine Game SDK together with the Unreal Engine Server SDK. The full LootLocker Server API is now at your disposal to be used for trusted and secure operations from your Game Server.
