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.
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 #
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(); }