Getting Started #

This guide covers setting up and logging into the Pragma backend using either the Pragma Unreal SDK or Unity SDK.

Unreal #

Prerequisites:

Set up the Player Controller and Game Mode Base #

  1. Open your Unreal project.
  2. Create a new “Player Controller” C++ class named MyPlayerController.
  3. Create a new “Game Mode Base” class named TutorialGameMode.

Set up the Player Controller files #

  1. Compile and run the Unreal editor from your IDE.
  2. Create a Blueprint class for MyPlayerController called MyPlayerControllerBP.
  3. Create a Blueprint class for TutorialGameMode C++ called TutorialGameModeBP.
  4. Open TutorialGameModeBP and set the Player Controller Class to MyPlayerControllerBP.
  5. Set up the MyPlayerController.h header file.
MyPlayerController.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "PragmaPtr.h"
#include "PragmaResult.h"
#include "MyPlayerController.generated.h"

// Forward declares Pragma pointer types using the PRAGMA_FWD macro supplied by `PragmaPtr.h`.
PRAGMA_FWD(FPlayer);

UCLASS()
class UNREALTUTORIAL_API AMyPlayerController : public APlayerController
{
    GENERATED_BODY()

public:

    virtual void BeginPlay() override;

private:

    Pragma::FPlayerPtr Player;
};
  1. Set up the Player Subsystem in your Player Controller.
MyPlayerController.cpp
#include "MyPlayerController.h"
#include "PragmaPlayer.h"
#include "PragmaLocalPlayerSubsystem.h"

void AMyPlayerController::BeginPlay()
{
    Super::BeginPlay();

    UE_LOG(LogTemp, Display, TEXT("Initializing Pragma SDK...") );

    const auto* Subsystem = GetLocalPlayer()->GetSubsystem<UPragmaLocalPlayerSubsystem>();

    // Player object represents the current player's session with the Pragma Backend and is 
    //created automatically by the `UPragmaLocalPlayerSubsystem`.
    Player = Subsystem->Player();
}

Implement LogIn and LogOut Exec Functions #

  1. Add login and logout methods and handlers to your Player Controller.
MyPlayerController.h
// ...

class UNREALTUTORIAL_API AMyPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
    virtual void BeginPlay() override;

    UFUNCTION(Exec)
    void LogIn(const FString& Username);

    UFUNCTION(Exec)
    void LogOut();

private:

    Pragma::FPlayerPtr Player;

    void HandleLoggedIn(const TPragmaResult<>& Result);
    void HandleLoggedOut();
};
MyPlayerController.cpp
// ...

void AMyPlayerController::LogIn(const FString& Username)
{
    Player->LogIn(
        EPragma_Account_IdProvider::UNSAFE,
        Username,
        Pragma::FPlayer::FLoggedInDelegate::CreateUObject(
            this, &AMyPlayerController::HandleLoggedIn));
}

void AMyPlayerController::LogOut()
{
    Player->LogOut(Pragma::FPlayer::FLoggedOutDelegate::CreateUObject(
        this, &AMyPlayerController::HandleLoggedOut));
}

void AMyPlayerController::HandleLoggedIn(const TPragmaResult<>& Result)
{
    if (Result.IsFailure())
    {
        UE_LOG(LogTemp, Display, TEXT("Pragma -- Login failed: %s"), *Result.ErrorCode());
        return;
    }
    UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged in."));
}

void AMyPlayerController::HandleLoggedOut()
{
    UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged out."));
}
  1. Recompile your project.

Hook up player controller to a new level #

  1. Hook up the TutorialGameModeBP to your new level:
    • Right click in the Content panel, create a new Level, and open that level.
    • In your Blueprint Project settings, set your GameMode to the TutorialGameModeBP.

Test #

Ensure Pragma Engine is running.

To test this functionality using the Unreal in-game console, call the exec function LogIn test01. This will call our LogIn() function with the username test01, which is an account that exists by default in the platform. You should see Pragma – Logged in. in your Unreal output log.

The test login method is disabled by default in production. It is called ‘unsafe’ because it has no password or other security measures and is intended only for development use.
To apply this functionality using Unreal Blueprints, create a button that calls your LogIn() or LogOut() function when clicked, as well as an input field where a player can enter their username.

Unity #

Prerequisites:

Create a C# MonoBehavior class #

  1. Open your Unity project.
  2. Use the Project pane to create a Scripts folder within the Assets folder.
  3. Right-click in the Scripts folder and create a new C# script named PragmaManager.
    • When you create the script directly from Unity, the file is populated with a Unity MonoBehaviour class.
  4. Replace the code with the following:
using Pragma;
using UnityEngine;

public class PragmaManager : MonoBehaviour
{
    public Pragma.Player Player { get; private set; }

    private void Awake()
    {
        // The Pragma Runtime is automatically initialized on first retrieval.
        var runtime = Pragma.Runtime.Get();

        // This returns the first Player if available, or creates it. The Runtime is the source of truth for Player objects.
        Player = runtime.Player();
    }
}

Facilitate login and logout #

To create a Pragma login/logout flow for your Unity project, add the following methods to your PragmaManager file:

public void LogIn()
{
    Player.LogIn(Pragma.Account.IdProvider.Unsafe, "test01", HandleLoggedIn);
}

public void LogOut()
{
    Player.LogOut(HandleLoggedOut);
}

private void HandleLoggedIn(Pragma.Result result)
{
    if (result.IsFailure)
    {
        Debug.LogErrorFormat("Pragma -- Login failed: {0}", result.ErrorCode);
        return;
    }
    Debug.Log("Pragma -- Logged in.");
}

private void HandleLoggedOut()
{
    Debug.Log("Pragma -- Logged out.");
}
  1. Open your project in the Unity editor.
  2. Right click in an empty part of the Hierarchy pane on the left side, click Create Empty, and name the GameObject PragmaManager.
  3. Drag the PragmaManager.cs script onto the PragmaManager GameObject.
  4. Right click in the Hierarchy pane and select UI then Button.
  5. Expand the Button object in the Hierarchy pane and select Text.
  6. In the Inspector pane, find the text box with the default text of Button. Rename it by typing Login.
  7. In the Hierarchy pane, click on the Button object.
  8. In the On Click (), select the + sign, and drag PragmaManager from the Hierarchy pane onto None (Object) in the Inspector pane. Click No Function and select PragmaManager, then Login().

Test #

In Unity, click play, then click the Login button to connect to your locally running Pragma Engine instance. If successful, you should see a Pragma -- Logged in. message in the Unity console.

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().