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 #
In your Unreal Editor create the following C++ classes.
- a
MyPlayerController
class with a “Player Controller” parent - a
MyGameModeBase
class with a “Game Mode Base” parent
Create Unreal Blueprints #
After the player controller and game mode base files are created, create the following 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.
- a
MyPlayerControllerBP
Blueprint based onMyPlayerController
- a
MyGameModeBaseBP
Blueprint based onMyGameModeBase
. Set the GameModeBaseBP’s Player Controller Class toMyPlayerControllerBP
.
Set up the player controller files #
Add the following code to your
MyPlayerController.h
header file.#pragma once #include "CoreMinimal.h" #include "GameFramework/PlayerController.h" #include "PragmaPtr.h" #include "Dto/PragmaPartyRpcExtDto.h" #include "MyPlayerController.generated.h" // Forward declares Pragma pointer types for Pragma::FPlayer. PRAGMA_FWD(FPlayer); UCLASS() class UNREALTUTORIAL_API AMyPlayerController : public APlayerController { GENERATED_BODY() public: //Called when Unreal level is loaded virtual void BeginPlay() override; private: // Pointer to our Pragma Player owned by the PRagmaLocalPlayerSubsystem. Pragma::FPlayerPtr Player; }
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("MyGameClient -- Game client startup.")); const auto* Subsystem = GetLocalPlayer() ->GetSubsystem<UPragmaLocalPlayerSubsystem>(); Player = Subsystem->Player(); }
Hook up player controller to a new level #
Ensure Pragma Engine is running.
Compile and open the Unreal project from your IDE.
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
MyGameModeBaseBP
option.d. In the World Settings panel under Game Mode ensure the Player Controller Class is set to
MyPlayerControllerBP
.
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.