Create Player Controller and Blueprint #

In this section, we’ll create the C++ player controller and associated Blueprints.

Prerequisites:

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 MyPlayerController. This creates the MyPlayerController.h header file and the MyPlayerController.cpp source file.

The generated header and source files should look similar to the following:

MyPlayerController.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

/**
 * 
 */
UCLASS()
class UNREALTUTORIAL_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()
	
};

MyPlayerController.cpp:

#include "MyPlayerController.h"

Create Blueprints #

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 MyPlayerController.

Writing the code #

Now that the Unreal project files are created, we can customize them to implement Pragma features. To start, we’ll define a BeginPlay() function and declare a few variables.

While you can copy and paste the code presented here, be aware that some values may have different names depending on your project. For example, our AMyPlayerController class was auto-generated with a “UNREALTUTORIAL_API macro”. Your file may use a different name depending on your Unreal project.
  1. Update your MyPlayerController.h header file to include the following:

    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/PlayerController.h"
    #include "MyPlayerController.generated.h"
    #include "PragmaPtr.h"
    #include "PragmaGameInstanceSubsystem.h"
    #include "PragmaResult.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:
    
        //Called when the `AMyPlayerController` is created.
        virtual void BeginPlay() override;
    
        //Additional Unreal functions can be added here
    
    private:
    
    	// Weak pointer to our Pragma Player owned by the PragmaLocalPlayerSubsystem.
        Pragma::FPlayerPtr Player;
        Pragma::FRuntimePtr Runtime;
    };
    
  2. Declare the BeginPlay() function in your MyPlayerController.cpp source file:

    #include "MyPlayerController.h"
    #include "PragmaPlayer.h"
    #include "Services/Party/PragmaParty.h"
    #include "PragmaLocalPlayerSubsystem.h"
    
    void AMyPlayerController::BeginPlay()
    {
        Super::BeginPlay();
    
        UE_LOG(LogTemp, Display, TEXT("Initializing") );
    
        const auto* Subsystem = GetLocalPlayer()->GetSubsystem<UPragmaLocalPlayerSubsystem>();
    
        // Main entrypoint into Pragma and automatically initialized in the PragmaGameInstanceSubsystem.
        auto RuntimeA = Subsystem->Runtime();
    
        // Set configuration for the SDK before logging in.
        RuntimeA->Config().BackendAddress = "http://127.0.0.1:10000";
        RuntimeA->Config().ProtocolType = EPragmaProtocolType::WebSocket;
        RuntimeA->Config().GameClientVersion = "GameServerVersion1";
    
        // Player object represents the current player's session with the Pragma Backend and is 
        //created automatically by the `UPragmaLocalPlayerSubsystem`.
        Player = Subsystem->Player();
    }