ryanjon2040
4 supporters
[C++] Connecting Epic Online Service wit ...

[C++] Connecting Epic Online Service with LootLocker

May 10, 2023

In this short and quick tutorial, you will learn how to create sessions in LootLocker using Epic Online Services. We will be using Redpoint (Free) EOS Online Subsystem Plugin as well.

I'm assuming you already have an account at LootLocker and Epic.

STEP I:

Download and install Redpoint EOS Online Subsystem plugin (both free and paid will work) to your Unreal Project. Then follow this to create your product and fill all the required details like client secret, ID etc.

IMPORTANT: Make sure Online Subsystem and Online Subsystem Utils plugins are enabled. If not, enable them and restart.


STEP II:

Login to your LootLocker console and assign your products Client ID.

STEP III:

After you've done all the previous steps correctly, let's write some C++ code. First thing to do is, add OnlineSubsystem and OnlineSubsystemUtils to your Build.cs file (this is required otherwise you'll get a compile error).

I'd recommend to create your own Game Instance Subsystem for this purpose. Assuming you have created one, let's adjust the header file like below.

UCLASS()
class YOURPROJECT_API UYourGameInstanceSubsystem : public UGameInstanceSubsystem
{
    GENERATED_BODY()

    FDelegateHandle DelegateHandle_LoginComplete;

    /** Access this in Blueprints */
    UPROPERTY(Transient, BlueprintReadOnly, meta = (AllowPrivateAccess = true))
    FString IdToken;

public:

    virtual void Initialize(FSubsystemCollectionBase& Collection) override;

private:

    void Internal_LoginToEOS();

    // Delegated call when login completes.
    void Internal_HandleLoginComplete(int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UserId, const FString& Error);
}

Then in your cpp file, add this:

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

void UYourGameInstanceSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
    GetWorld()->GetTimerManager().SetTimerForNextTick(this, &ThisClass::Internal_LoginToEOS);
}

void UYourGameInstanceSubsystem::Internal_LoginToEOS()
{
    IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
    IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();

    static constexpr const uint8 UserIdx = 0;
    if (Identity->GetLoginStatus(UserIdx) == ELoginStatus::LoggedIn)
    {
        auto UserId = Identity->GetUniquePlayerId(0);
        auto UserAccount = Identity->GetUserAccount(*UserId.Get());
        UserAccount->GetAuthAttribute("epic.idToken", IdToken);
        return;
    }

    DelegateHandle_LoginComplete = Identity->AddOnLoginCompleteDelegate_Handle(0, FOnLoginComplete::FDelegate::CreateUObject(this, &ThisClass::Internal_HandleLoginComplete));

    if (!Identity->AutoLogin(UserIdx))
    {
              // Auto login failed.
    }
}

void UYourGameInstanceSubsystem::Internal_HandleLoginComplete(int32 LocalUserNum, 
    bool bWasSuccessful, 
    const FUniqueNetId& UserId, const FString& Error)
{
    IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
    IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
    
    if (!bWasSuccessful)
    {
        // Login complete but was not successful. Log your error here.
    }
    else
    {
        auto UserAccount = Identity->GetUserAccount(UserId);
        UserAccount->GetAuthAttribute("epic.idToken", IdToken);
    }

    Identity->ClearOnLoginCompleteDelegate_Handle(LocalUserNum, DelegateHandle_LoginComplete);
    DelegateHandle_LoginComplete.Reset();
}

That's it! Now you are good to go ✌

You can Start Epic Session from anywhere in your game. Typically this will be your Main Menu UMG widget so create the following graph connected to Event Construct in UMG.

NOTE: Instead of Agora Game Subsystem node, use your own Game Instance Subsystem that you created in Step III. If you have done everything correctly, your player will be automatically signed in and create session in Loot Locker like this:

Enjoy this post?

Buy ryanjon2040 a coffee

More from ryanjon2040