Initialize Pragma Services #

In this section, we’ll initialize the Pragma Party and Game Instance services that we’ll use in the other Unity tutorials.

Some Pragma services require calling an Initialize() method to prepare a service for use, for example to pre-populate caches. These service initialization methods should be invoked once after successfully logging into Pragma.

Prerequisites:

Create an Initialize function #

In PragmaManager.cs, add an InitializeServices() method that uses the Initialize() methods for each Pragma service:

public void InitializeServices()
{
    // Initialize Party service after logging in
    Player.PartyApi.Initialize(result =>
    {
        if (result.IsFailure)
        {
            Debug.Log("Pragma -- Init failed");
        }
        else 
        {
            Debug.Log("Pragma -- Party service initialized");
        }
    });

    // Initialize Game Instance service after logging in
    Player.GameInstanceApi.Initialize(result =>
    {
        if (result.IsFailure)
        {
            Debug.Log("Pragma -- Game instance API initialization failed");
        }
        else 
        {
            Debug.Log("Pragma -- Game instance API initialized");
        }
    });
}

Initialize upon login #

In the PragmaManager.cs HandleLoggedIn() method, add a call to InitializeServices() if login is successful:

private void HandleLoggedIn(Pragma.Result<string> result)
{
    if (result.IsFailure)
    {
        Debug.LogErrorFormat("Pragma -- Login failed: {0}", result.ErrorCode);
    }
    else
    {
        Pragma.PragmaId myPragmaId = Player.Account.PragmaPlayerId;
        string myPragmaIdString = myPragmaId.ToString();
        Debug.Log("Pragma -- Logged in. Pragma ID: " + myPragmaIdString);

        // If login successful, initialize services
        InitializeServices();
    }
}

Run the game #

  1. Ensure Pragma is running via ./pragma run or the Intellij run-pragma run configuration.
  2. Run the game.
  3. Log in to the Pragma backend.
  4. Confirm the Party Service was initialized successfully.
    • Log: Pragma -- Party service initialized
  5. Confirm the Game Instance Service was initialized successfully.
    • Log: Pragma -- Game instance API initialized

Recap #

You’ve successfully initialized the Pragma Party and Game Instance services. From here check out the provided how-to guides and tutorials for examples on how to use various Pragma services. Good luck and have fun!