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, selectMyPlayerController
.
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 "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; //Additional Unreal functions can be added here private: // Weak pointer to our Pragma Player owned by the PragmaLocalPlayerSubsystem. Pragma::FPlayerPtr Player; Pragma::FRuntimePtr Runtime; };
Add the following code to 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(); }
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 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.