Logging Into the Platform #

In this section, we’ll be logging in with the configured SDK. To test that our SDK is configured correctly, we use a test login.

Create the login script #

  1. Open a terminal and return to platform as the working directory. Run make run to start Pragma Engine.
  • Alternatively, Pragma Engine can be run from IntelliJ. From the IntelliJ toolbar in the upper right, ensure MainKt - LocalConfigured is selected, then click the play button.
  • If not, click Add Configuration…. In the Run/Debug Configurations window that appears, expand Kotlin in the left hand side, then select MainKt - LocalConfigured. Click OK. Click the play button in the IntelliJ toolbar to start Pragma Engine.
  1. Once the engine has started successfully, it prints the message [main] INFO main - Pragma server startup complete.

  2. In Rider, locate and open [Project Name]GameModeBase.h and [Project Name]GameModeBase.cpp.

  3. In [Project Name]GameModeBase.h confirm your imports match the following. Make sure you update the final #include to reflect your project name.

    #include "CoreMinimal.h"
    #include "PragmaConnection.h"
    #include "PragmaResult.h"
    #include "PragmaSession.h"
    #include "GameFramework/GameModeBase.h"
    #include "[Project Name]GameModeBase.generated.h"
    
  4. Also in [Project Name]GameModeBase.h, add the following to the A[Project Name]GameModeBase class below GENERATED_BODY():

    virtual void BeginPlay() override;
    
    public:
    void Init(ULocalPlayer& InLocalPlayer);
    
    protected:
    
    UFUNCTION(Exec)
    void LogIn(const FString& Username);
    
    private:
    void HandleConnected();
    void HandleLoggedIn(const TPragmaResult<FString>& Result);
    void HandleDisconnected(TOptional<UPragmaConnection::FConnectionError> Error);
    
    private:
    UPROPERTY()
    UPragmaSession* Session;
    
    UPROPERTY()
    ULocalPlayer* LocalPlayer;
    
Detailed explanation:

This section declares BeginPlay(), which is called when the GameMode is started.

Init() gets the LocalPlayer’s Session SubUPragmaSession* system and initializes the object used to connect to the backend.

HandleConnected, HandleLoggedIn, and HandleDisconnected are all methods we bind to these events.

  1. Reference the LocalPlayer’s session from the SessionSubsystem by pasting the following code in the default project GameMode cpp file called [Project Name]GameModeBase.cpp.
You will need to replace [Project Name] with your project’s name.
#include "[Project Name]GameModeBase.h"

#include "PragmaSessionLocalPlayerSubsystem.h"
#include "PragmaSession.h"

void A[Project Name]GameModeBase::BeginPlay()
{
    UE_LOG(LogTemp, Warning, TEXT("Initializing") );
    Init(*GetWorld()->GetFirstLocalPlayerFromController());
}

void A[Project Name]GameModeBase::Init(ULocalPlayer& InLocalPlayer)
{
    LocalPlayer = &InLocalPlayer;
    auto* World = GetWorld();
    if (!World->IsClient())
    {
        return;
    }
    auto* SessionSubsystem = LocalPlayer->GetSubsystem<UPragmaSessionLocalPlayerSubsystem>();
    if (!ensure(SessionSubsystem))
    {
        return;
    }
    Session = &SessionSubsystem->Session();
    Session->Connection()->OnConnected.AddUObject(this, &A[Project Name]GameModeBase::HandleConnected);
    Session->Connection()->OnDisconnectedV2.AddUObject(this, &A[Project Name]GameModeBase::HandleDisconnected);

    LogIn("test01");
}

void A[Project Name]GameModeBase::LogIn(const FString& Username)
{
    UPragmaAccountService::FLoggedInDelegate OnLoggedIn;
    OnLoggedIn.AddUObject(this, &A[Project Name]GameModeBase::HandleLoggedIn);
    Session->Account().LogIn(EPragma_Account_IdProvider::UNSAFE, Username, OnLoggedIn);
}

void A[Project Name]GameModeBase::HandleConnected()
{
    UE_LOG(LogTemp, Warning, TEXT("Pragma Connected") );
}

void A[Project Name]GameModeBase::HandleDisconnected(TOptional<UPragmaConnection::FConnectionError> Error)
{
    UE_LOG(LogTemp, Warning, TEXT("Pragma Disconnected") );
}

void A[Project Name]GameModeBase::HandleLoggedIn(const TPragmaResult<FString>& Result)
{
    if (Result.IsSuccessful())
    {
        UE_LOG(LogTemp, Warning, TEXT("Pragma Logged in! Player Id: %s:"), *Session->Account().GetPragmaId());
    }
    else
    {
        // Note you can access platform/sdk error enums in Result as well.
        UE_LOG(LogTemp, Warning, TEXT("Pragma Disconnected") );
    }
}
Detailed explanation:

BeginPlay is called when the GameMode is loaded.

Init creates the Replace with: PragmaSession object which uses the BackendAddress stored in the DefaultGame.ini file to connect to the Pragma service.

Once connected to Pragma Engine, you log in.

Log in #

  1. Compile the project using the green hammer icon at the top of the Rider window.

  2. Switch back to Unreal Editor, then click World Settings on the righthand side of the window. Under Game Mode click the GameMode Override dropdown and select the [Project Name]GameModeBase option.

  3. Save and click the Play icon at the top of the window.

  4. Open the Output Log from Window -> Developer Tools -> Output log to confirm the login was a success.

  5. Navigate to http://localhost:10200/ to load Pragma Portal. Select Sign in with Pragma Unsafe and type in test04.

An Unsafe login is a test login that is disabled by default before launching into production. To enable this login, a configuration called Unsafe would have to be switched on; the name should provide a warning that the system is using an unsafe login only intended for testing.
  1. Find the entry with the Player id for test01 from when you logged in with test01 via Unreal.
Once logged in, you can call other Pragma API calls from Session->*Service Name*().Action.