# Steam

{% hint style="warning" %}
To use Steam in your game you must be a registered developer and have a game with [SteamWorks](https://partner.steamgames.com/)
{% endhint %}

#### Configure Steam in LootLocker

Go to [Platform Settings](https://console.lootlocker.com/settings/platforms) in the LootLocker Web Console and make sure the Steam platform is enabled.

![LootLocker Steam Settings](https://534367586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVu1MPzezO-NgvC98xh%2Fuploads%2Fgit-blob-cd4fba3a6a785d785b80ab7ee4eda0e33cebe8f2%2Fsteam-settings-red.png?alt=media\&token=d1ceac1a-5ce2-450e-9b09-632d883d19d5)

**Steam App ID**

To get the Steam App ID you have to log in to the SteamWorks Partner Dashboard.

![The App ID is behind the red square](https://534367586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVu1MPzezO-NgvC98xh%2Fuploads%2Fgit-blob-3fd55238f6c44da2c1f9c0bde721f151a8b16654%2Fsteam-app-id.png?alt=media\&token=0f364036-c8a9-4ada-ac54-b2918c84f50e)

**Steam Publisher Key**

To find the Steam Publisher Key you can follow [this guide](https://partner.steamgames.com/doc/webapi_overview/auth) from Valve.

#### Install SteamWorks in Your Project

Before we can authenticate with Steam and start a LootLocker session we must have access to a few values from the SteamWorks API.

To learn more about authentication with Steam you can read their documentation for [Session Tickets in SteamWorks](https://partner.steamgames.com/doc/features/auth#client_to_backend_webapi).

{% tabs %}
{% tab title="Unity" %}
Our recommended way to gain access to the SteamWorks API in a Unity Game is by using the 3rd party library called **Steamworks.NET**

Install **Steamworks.NET** using the instructions found here: <http://steamworks.github.io/installation/>
{% endtab %}

{% tab title="Unreal" %}
To learn now to configure your game to work with Steam, please follow the directions found here:

* [Unreal Documentation v4.27 and below](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/Online/Steam/)
* [Unreal Documentation v5.0 and up](https://docs.unrealengine.com/5.0/en-US/online-subsystem-steam-interface-in-unreal-engine/)
  {% endtab %}

{% tab title="Godot" %}
To learn now to configure your game to work with Steam, please follow the [directions from the official GodotSteam documentation](https://godotsteam.com/).
{% endtab %}
{% endtabs %}

#### Authenticate Player

After adding SteamWorks integration in your project you can go ahead and authenticate the player. If successful, this call will return a lot of data that you can use to display to the Player or make more calls to LootLocker during this session.

{% tabs %}
{% tab title="Unity" %}
We'll start by creating a new empty Game Object in your scene and calling it GameManager. Feel free to skip this if you already have a GameManager or similar in your game.

![Creating empty GameObject](https://534367586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVu1MPzezO-NgvC98xh%2Fuploads%2Fgit-blob-4e090a11374e7a13449de3fd27a65d56c8e2bdf8%2Fcreate_empty_game_object.png?alt=media\&token=1107d48e-2811-4f90-a9ba-3616980023d9)

In this new GameObject you can add a new script called GameManager.

![Create new script on GameObject](https://534367586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVu1MPzezO-NgvC98xh%2Fuploads%2Fgit-blob-e97c45c6d0c253c31500167cc0cb8aad51013a44%2Fadd-game-manager-script.png?alt=media\&token=e7d2ef71-ca71-44de-ba4d-024eed2a23db)

Open up your new script in your editor of choice and add the following code:

```csharp
using LootLocker.Requests;
using Steamworks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    // Consider offsetting this from the start method to avoid startup race conditions
    void Start()
    {
        // To make sure Steamworks.NET is initialized
       if (!SteamManager.Initialized)
        {
            return;
        }
        
        var ticket = new byte[1024];
        var networkIdentity = new SteamNetworkingIdentity();
        HAuthTicket ticketResult = SteamUser.GetAuthSessionTicket(ticket, ticket.Length, out uint actualTicketSize, ref networkIdentity);
        if (ticket.Equals(HAuthTicket.Invalid))
        {
            Debug.LogWarning("Ticket from Steam was invalid: " + ticket.ToString());
            return;
        }

        LootLockerSDKManager.VerifyPlayerAndStartSteamSession(ref ticket, actualTicketSize, (response) =>
        {
            if (!response.success)
            {
                Debug.Log("Error starting a LootLocker session from the Steam User: " + response.errorData.ToString());
                return;
            }
            
            Debug.Log("Successfully started a LootLocker session from Steam User with ID: " + SteamID.ToString());
        });
	}
}
```

{% endtab %}

{% tab title="Unreal" %}
**Retrieve the Steam Session Ticket**

The following steps will help you to retrieve the `SteamSessionTicket` from the Online Subsystem that you set up in [the previous step](#install-steamworks-in-your-project).

Create a new UClass called `USteamSessionHelper` like this (this is the .h file):

```cpp
#pragma once

#include "CoreMinimal.h"

#include "SteamSessionHelper.generated.h"

UCLASS(Blueprintable)
class USteamSessionHelper : public UObject
{
    GENERATED_BODY()
public:
    UFUNCTION(BlueprintCallable, CallInEditor, Category = "<YourProjectName> | SteamSessionHelper")
    static FString GetSteamSessionTicket(int LocalUserNumber);
};
```

Paste the following code in the `.cpp` file:

{% code fullWidth="false" %}

```cpp
// Copyright (c) 2021 LootLocker

#include "SteamSessionHelper.h"

#include "Interfaces/OnlineIdentityInterface.h"
#include "OnlineSubsystem.h"

FString USteamSessionHelper::GetSteamSessionTicket(int LocalUserNumber)
{
    const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get(STEAM_SUBSYSTEM);
    if (OnlineSubsystem == nullptr || OnlineSubsystem->GetSubsystemName() != STEAM_SUBSYSTEM)
    {
	//Handle error: "Could not get Steam Online Subsystem"
	return "";
    }

    const IOnlineIdentityPtr IdentityInterface = OnlineSubsystem->GetIdentityInterface();
    if (IdentityInterface == nullptr || !IdentityInterface.IsValid())
    {
	//Handle error: "Could not get Steam Online Subsystem Identity Interface"
	return "";
    }
    
    if(IdentityInterface->GetLoginStatus(LocalUserNumber) != ELoginStatus::LoggedIn)
    {
        //Handle error: "Player is not logged in"
	return "";
    }

    return IdentityInterface->GetAuthToken(LocalUserNumber);
}
```

{% endcode %}

Generate Visual Studio project files and build the project.

You can now use these new methods to get the Steam session ticket from either blueprints or code.

**Verify the Player and Start a LootLocker session**

In Blueprint you can retrieve the token by finding the node you just made above: `GetSteamSessionTicket` in the `<YourProjectName> | SteamSessionHelper` category. This is static so you can find it by right-clicking anywhere within the Event Graph.

Then you can pass the `SteamSessionTicket` to the `Start Steam Session Using Ticket` node which you can find by right-clicking anywhere within the Event Graph.

<figure><img src="https://534367586-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVu1MPzezO-NgvC98xh%2Fuploads%2Fgit-blob-fea024a732db196f69835e1ead6119d7eaa67aa1%2FSteamVerifyPlayerAndStartSession.png?alt=media" alt="Example implementation in Unreal Blueprints for Steam Player Verification and Session Start using LootLocker - https://blueprintue.com/blueprint/lup-8wln/"><figcaption><p><a href="https://blueprintue.com/blueprint/lup-8wln/">Blueprint example of verifying a steam player and starting a steam session</a></p></figcaption></figure>

To copy and paste the above example into your editor, [look here](https://blueprintue.com/blueprint/lup-8wln/).

**Input**

You need to exchange the `TriggerSteamAuthentication` event for whatever event you want to use to trigger the login flow.

This example does not include the nodes you made above to get the steam id and session ticket. So make sure to utilize those if you made them. If you're getting the values another way, then that's fine. Simply plug the values into the Verify Player and Start Steam Session node.

**Output**

We recommend branching the completed events on the success flag, and if you do this you will probably want to add error handling in case the request fails as well as what (if any) continued actions you want on success.

The session response on a successful call also contains a lot of interesting information that you likely want to save, such as `player_id`, `public_uid`, and `player_ulid` among others.

{% hint style="warning" %}
You can not test authentication with Steam in the editor. You need to package your game before it can connect to Steam.
{% endhint %}
{% endtab %}

{% tab title="Godot" %}
Follow the [guide from GodotSteam](https://godotsteam.com/tutorials/) on how to [initialize Steam](https://godotsteam.com/tutorials/initializing/). Once Steam is properly initialized, open up the script from which you want to trigger LootLocker Authentication in Godot and add the following code.

```gdscript

var auth_ticket = Steam.getAuthSessionTicket()
if !auth_ticket.has('buffer'):
    printerr("Steam Auth Session Ticket retrieval failed")
    # Handle error as fit in your code
    
var authTicketString : String = LL_Authentication.ParseSteamAuthTicket(auth_ticket['buffer'], auth_ticket['size'])
if authTicketString.is_empty():
    printerr("Auth ticket could not be parsed")
    # Handle error as fit in your code
    
var steamLoginResponse = await LL_Authentication.SteamSession.new(authTicketString).send()
if(!steamLoginResponse.success) :
    printerr("Login failed with reason: " + steamLoginResponse.error_data.to_string(), true)
    # Handle error as fit in your code
print("Successfully started Steam session with LootLocker")
```

To confirm that everything is running without errors, you can start your game and check the console for the correct message in the log.
{% endtab %}
{% endtabs %}

{% hint style="success" %}
Congratulations - you have now started using LootLocker in your game with Steam! Next up we suggest you look at our [feature set,](https://docs.lootlocker.com/the-basics/what-is-lootlocker#overview) and decide which ones you want to use in your game.
{% endhint %}


---

# 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/players/authentication/how-to/steam.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.
