Initialize Pragma Services #

In this section, we’ll initialize the various Pragma services that we’ll use in the other Unreal tutorials.

Prerequisites:

Create an Initialize function #

Goal: Create an InitializeServices() method that initializes the Pragma Party, Game Instance, Friends, and Presence services.

The PartyApi, GameInstanceApi, FriendsApi, and PresenceApi Pragma SDKs for Unreal provide Initialize() functions for the various Pragma services. Use these functions to initialize the services in your player controller.

  1. In MyPlayerController.h, declare the InitializeServices() method:

    private:
        void InitializeServices();
    
  2. In MyPlayerController.cpp, define InitializeServices():

    void AMyPlayerController::InitializeServices()
    {
        UPragmaPartyApi::FOnCompleteDelegate PartyInitializeDelegate;
        PartyInitializeDelegate.BindWeakLambda(
            this, [this](const TPragmaResult<>& PartyResult)
            {
                if (PartyResult.IsSuccessful())
                {
                    UE_LOG(LogTemp, Display,
                        TEXT("MyGameClient -- Party service initialized."));
                }
                else
                {
                    UE_LOG(LogTemp, Error,
                        TEXT("MyGameClient -- Party service failed to initialize: %s"),
                        *PartyResult.GetErrorAsString());
                }
            });
    
        UPragmaGameInstanceApi::FOnCompleteDelegate GameInstanceInitializeDelegate;
        GameInstanceInitializeDelegate.BindWeakLambda(
            this, [this](const TPragmaResult<>& GameResult)
            {
                if (GameResult.IsSuccessful())
                {
                    UE_LOG(LogTemp, Display,
                        TEXT("MyGameClient -- Game instance service initialized."));
                }
                else
                {
                    UE_LOG(LogTemp, Error,
                        TEXT("MyGameClient -- Game instance failed to initialize: %s"),
                        *GameResult.GetErrorAsString());
                }
            });
    
        UPragmaFriendApi::FOnCompleteDelegate FriendInitializeDelegate;
        FriendInitializeDelegate.BindWeakLambda(
            this, [this](const TPragmaResult<>& FriendResult)
            {
                if (FriendResult.IsSuccessful())
                {
                    UE_LOG(LogTemp, Display,
                        TEXT("MyGameClient -- Friend service initialized."));
                }
                else
                {
                    UE_LOG(LogTemp, Error,
                        TEXT("MyGameClient -- Friend service failed to initialize: %s"),
                        *FriendResult.GetErrorAsString());
                }
            });
    
        UPragmaPresenceApi::FOnCompleteDelegate PresenceInitializeDelegate;
        PresenceInitializeDelegate.BindWeakLambda(
            this, [this](const TPragmaResult<>& PresenceResult)
            {
                if (PresenceResult.IsSuccessful())
                {
                    UE_LOG(LogTemp, Display,
                        TEXT("MyGameClient -- Presence service initialized."));
                }
                else
                {
                    UE_LOG(LogTemp, Error,
                        TEXT("MyGameClient -- Presence service failed to initialize: %s"),
                        *PresenceResult.GetErrorAsString());
                }
            });
    
        Player->PartyApi().Initialize(PartyInitializeDelegate);
        Player->GameInstanceApi().Initialize(GameInstanceInitializeDelegate);
        Player->FriendApi().Initialize(FriendInitializeDelegate);
        Player->PresenceApi().Initialize(PresenceInitializeDelegate);
    }
    

Initialize upon login #

Goal: Initialize services when a user logs in.

In the MyPlayerController.cpp LogIn() method, add a call to InitializeServices() if login is successful:

void AMyPlayerController::LogIn(const FString& Username)
{
	Player->LogIn(
		EPragma_Account_IdProvider::UNSAFE,
		Username,
		Pragma::FPlayer::FLoggedInDelegate::CreateWeakLambda(
			this, [=](const TPragmaResult<>& Result)
			{
				if (Result.IsSuccessful())
				{
					UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Logged in."));
					InitializeServices();
				}
				else
				{
					UE_LOG(LogTemp, Error, TEXT("MyGameClient -- Login failed: %s"),
					       *Result.GetErrorAsString());
				}
			}));
}