# Starting a Server Session

In this How-To we'll look at how to start and maintaing a Server Session in Unreal Engine, using the [Unreal Server SDK](https://github.com/lootlocker/unreal-server-sdk) in C++.

### Prerequisites

* [A LootLocker account](https://lootlocker.com/sign-up)
* [A created game in the web console](https://docs.lootlocker.com/)
* An Unreal *Server* Project
* [LootLocker Unreal Server SDK](https://github.com/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.

{% hint style="warning" %}
The code in this **How-To** is made to be as transparent and clear as possible, not performant, adapt the code to your own Server as needed.
{% endhint %}

{% tabs %}
{% tab title="Unreal C++" %}

```c
#include "ServerSession.h"
#include "LootLockerServerForCpp.h"
#include "Templates/SharedPointer.h"
#include "TimerManager.h"

// Timer handle to maintain the session
FTimerHandle 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);
}
```

{% endtab %}

{% tab title="Unreal Blueprints" %}

<figure><img src="/files/Wliwr6F9TZoq34CSjGld" alt=""><figcaption><p><a href="https://blueprintue.com/blueprint/tjoyy0f5/">Blueprint for Starting a Server Session</a></p></figcaption></figure>

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.
{% endtab %}
{% endtabs %}

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](https://github.com/lootlocker/unreal-server-sdk). You can use the [Unreal Engine Game SDK](https://github.com/lootlocker/unreal-sdk) together with the [Unreal Engine Server SDK](https://github.com/lootlocker/unreal-server-sdk). The full [LootLocker Server API](https://ref.lootlocker.com/server) is now at your disposal to be used for trusted and secure operations from your Game Server.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lootlocker.com/server/unreal-server/how-to/unreal-server-start-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
