Create Player Controller and Blueprint #

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

Prerequisites:

Create C++ player controller files #

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.

Create Unreal 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.

  • Create a new Blueprint class by right-clicking anywhere in the Content Browser. 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.

Set up the player controller files #

  1. Add the following code to your MyPlayerController.h header file:

    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/PlayerController.h"
    #include "PragmaPtr.h"
    #include "PragmaGameInstanceSubsystem.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:
    
        //Called when the `AMyPlayerController` is created.
        virtual void BeginPlay() override;
    
    private:
    
    	// Weak pointer to our Pragma Player owned by the PragmaLocalPlayerSubsystem.
        Pragma::FPlayerPtr Player;
    };
    
  2. Add the following code to your MyPlayerController.cpp source file:

    #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();
    }
    
    When configuring the SDK for a game server, replace Subsystem->Player() with Subsystem->Server().

Hook up player controller to a new level #

  1. Ensure Pragma Engine is running.

  2. Compile and open the Unreal project from your IDE.

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

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

    b. Open the Window menu and click World Settings.

    c. In the World Settings panel under Game Mode click the GameMode Override dropdown and select the GM_TutorialGameModeBase option.

    d. In the World Settings panel under Game Mode ensure the Player Controller Class is set to MyPlayerController.

Next steps #

You now have a basic Unreal project with all the items needed to begin integrating Pragma features. In the next topic we’ll add login and logout functionality using the player controller classes and Pragma SDK.