Log in to the Platform #

In this section, we’ll be logging in with the configured SDK using a test user.

Create C++ classes #

Open your Unreal project. In the Unreal Editor Content Browser under C++ Classes, create a new C++ player controller and name it PC_TutorialPlayerController. After the player controller is created, right-click on it in the Content Browser and create a Blueprint class based on it. We recommend creating this Blueprint class in a new directory dedicated to Blueprints.

Next, create another Blueprint class. Choose Game Mode Base as the parent, and name this class GM_TutorialGameModeBase. After it’s created, double-click it to bring up the Class Defaults view. Under Player Controller Class, select PC_TutorialPlayerController.

Writing the code #

For this example, we will put code in our PC_TutorialPlayerController class.

  1. In your project, create the files PC_TutorialPlayerController.h and PC_TutorialPlayerController.cpp.

  2. Add the following code to PC_TutorialPlayerController.h

    #pragma once
    #include "CoreMinimal.h"
    #include "GameFramework/PlayerController.h"
    #include "PragmaResult.h"
    // Provides the PRAGMA_FWD macro.
    #include "PragmaPtr.h"
    #include "PC_TutorialPlayerController.generated.h"
    // Forward declares Pragma pointer types for Pragma::FPlayer.
    class APC_TutorialPlayerController : public APlayerController
        virtual void BeginPlay() override;
        void LogIn(const FString& Username);
        void LogOut();
        void HandleLoggedIn(const TPragmaResult<>& Result);
        void HandleLoggedOut();
        // Weak pointer to our Pragma Player owned by the PragmaLocalPlayerSubsystem.
        Pragma::FPlayerPtr Player;
Method overview

This section forward declares Pragma pointer types using the PRAGMA_FWD macro supplied by PragmaPtr.h.

BeginPlay() is called when the APC_TutorialPlayerController is created.

LogIn and LogOut methods are annotated with UFUNCTION(Exec) so that you can easily call them from the in-game console for the sake of testing.

The Player class member is a weak pointer to the object that represents Player’s session to the Pragma Backend.

  1. Add the following code to PC_TutorialPlayerController.cpp

    #include "PC_TutorialPlayerController.h"
    #include "PragmaLocalPlayerSubsystem.h"
    #include "PragmaResult.h"
    #include "PragmaRuntime.h"
    #include "PragmaPlayer.h"
    void APC_TutorialPlayerController::BeginPlay()
        const auto* Subsystem = GetLocalPlayer()->GetSubsystem<UPragmaLocalPlayerSubsystem>();
        // The Pragma Runtime is automatically initialized in the PragmaGameInstanceSubsystem.
        auto Runtime = Subsystem->Runtime();
        // Set configuration for the SDK before logging in.
        Runtime->Config().BackendAddress = "";
        Runtime->Config().ProtocolType = EPragmaProtocolType::WebSocket;
        Runtime->Config().GameClientVersion = "GameServerVersion1";
        // The UPragmaLocalPlayerSubsystem is automatically initialized
        // with a Pragma Player object for every LocalPlayer.
        Player = Subsystem->Player();
        // Add listeners connection-related events as desired.
        Player->OnConnected.AddLambda([this]{ /* ... */ });
        Player->OnDisconnected.AddLambda([this]{ /* ... */ });
        Player->OnDegraded.AddLambda([this]{ /* ... */ });
        Player->OnReconnected.AddLambda([this]{ /* ... */ });
    void APC_TutorialPlayerController::LogIn(const FString& Username)
                this, &APC_TutorialPlayerController::HandleLoggedIn));
    void APC_TutorialPlayerController::LogOut()
            this, &APC_TutorialPlayerController::HandleLoggedOut));
    void APC_TutorialPlayerController::HandleLoggedIn(const TPragmaResult<>& Result)
        if (Result.IsFailure())
            UE_LOG(LogTemp, Display, TEXT("Pragma -- Login failed: %s"), *Result.ErrorCode());
        UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged in."));
    void APC_TutorialPlayerController::HandleLoggedOut()
        UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged out."));
Method overview

BeginPlay() is called when the APC_TutorialPlayerController is created.

The PragmaRuntime is the main entrypoint into Pragma. In this case we use it to set necessary configuration.

The Player object is created automatically by the UPragmaLocalPlayerSubsystem, this represents the current player’s session with the Pragma Backend.

The various events like OnConnected and OnDisconnected are major lifecycle events we can listen for on the Player session.

The LogIn and LogOut events simply forward to the Player object’s equivalent LogIn and LogOut methods, including a lambda callback that will be invoked when on completion.

In the case of LogIn, we get back a TPragmaResult, which is a fancy way of saying “did it work?” We can inspect the Result.IsSuccessful() or Result.IsFailure() to answer that question. If it errors, we can use the ErrorCode() method to get a string to log.

Try it out #

  1. Run Pragma Engine via one of the following methods.
Running via Make
Run make run to start the platform. Run this in a terminal with platform as the working directory.
Running in IntelliJ

From the IntelliJ toolbar in the upper right, ensure MainKt - LocalConfigured is selected, then click the play button.

If MainKt - LocalConfigured isn’t available, you will need to configure it. In the IntelliJ toolbar, click the dropdown next to the run button, then click Edit Configurations…. 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.

Once the engine has started successfully, it prints the message [main] INFO main - Pragma server startup complete.

  1. Compile the Unreal project from your IDE.

  2. Open your project in the Unreal Editor.

  3. Hook up the new PC_TutorialPlayerController class as the default PlayerController for your game mode and level.

    1. Right click in the Content panel, create a new Level, and open that level. (Just so we don’t edit any of your existing levels.)
    2. Open the Window menu and click World Settings.
    3. In the World Settings panel under Game Mode click the GameMode Override dropdown and select the GM_TutorialGameModeBase option.
    4. In the World Settings panel under Game Mode ensure the Player Controller Class is set to PC_TutorialPlayerController.

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

  5. Click on the game window to focus it, open the in-game console with ~, and type LogIn test01. This will call our LogIn method with the username test01, which is an account that exists by default in the platform.

  6. Open the Output Log from Window -> Output Log (Window -> Developer Tools -> Output Log in Unreal 4) to confirm the login was a success. You should see a line like LogTemp: Display: Pragma -- Logged in.

The Unsafe login method is disabled by default in production. It is ‘unsafe’ because it has no password or other security measures!
Once logged in, you can call other Pragma API calls like this: Player->*Service Name*().Action().