Create Player Controller and Game Mode Classes #

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

Prerequisites:

Create C++ files #

Open your Unreal project. In the Unreal Editor Content Browser under C++ Classes, right click anywhere in the content browser and create the following C++ classes:

  • a MyPlayerController class with a “Player Controller” parent
  • a TutorialGameMode class with a “Game Mode Base” parent

Unreal will add the associated C++ header and source files to the project.

Create Unreal Blueprints #

After the player controller and game mode base are created, create two Blueprint classes, one based on the player controller, and one based on the game mode. We recommend saving these Blueprint classes in a new directory dedicated to Blueprints.

  1. Right-click on the MyPlayerController C++ class in the Content Browser and create a Blueprint class based on it. Call the class MyPlayerControllerBP.

  2. Right-click on the TutorialGameModeBase C++ class in the Content Browser and create a Blueprint class based on it. Call the class TutorialGameModeBaseBP. 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. Make sure the class name reflects the name of your Unreal project. For example, our project is called UNREALTUTORIAL so the class is UNREALTUTORIAL_API.

    #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 Player Controller 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 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.