Log in to the Platform #
In this section, we’ll be logging in with the configured SDK using a test user.
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 PC_TutorialPlayerController
. 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 PC_TutorialPlayerController
.
Writing the code #
For this example, we will put code in our PC_TutorialPlayerController
class.
In your project, create the files
PC_TutorialPlayerController.h
andPC_TutorialPlayerController.cpp
.Add the following code to
PC_TutorialPlayerController.h
#pragma once #include "CoreMinimal.h" #include "GameFramework/PlayerController.h" #include "PragmaResult.h" // Provides the PRAGMA_FWD macro. #include "PragmaPtr.h" #include "PC_TutorialPlayerController.generated.h" // Forward declares Pragma pointer types for Pragma::FPlayer. PRAGMA_FWD(FPlayer); UCLASS() class APC_TutorialPlayerController : public APlayerController { GENERATED_BODY() public: virtual void BeginPlay() override; UFUNCTION(Exec) void LogIn(const FString& Username); UFUNCTION(Exec) void LogOut(); private: void HandleLoggedIn(const TPragmaResult<>& Result); void HandleLoggedOut(); // Weak pointer to our Pragma Player owned by the PragmaLocalPlayerSubsystem. Pragma::FPlayerPtr Player; };
Add the following code to
PC_TutorialPlayerController.cpp
#include "PC_TutorialPlayerController.h" #include "PragmaLocalPlayerSubsystem.h" #include "PragmaResult.h" #include "PragmaRuntime.h" #include "PragmaPlayer.h" void APC_TutorialPlayerController::BeginPlay() { Super::BeginPlay(); const auto* Subsystem = GetLocalPlayer()->GetSubsystem<UPragmaLocalPlayerSubsystem>(); // The Pragma Runtime is automatically initialized in the PragmaGameInstanceSubsystem. auto Runtime = Subsystem->Runtime(); // Set configuration for the SDK before logging in. Runtime->Config().BackendAddress = "http://127.0.0.1:10000"; Runtime->Config().ProtocolType = EPragmaProtocolType::WebSocket; Runtime->Config().GameClientVersion = "GameServerVersion1"; // The UPragmaLocalPlayerSubsystem is automatically initialized // with a Pragma Player object for every LocalPlayer. Player = Subsystem->Player(); // Add listeners connection-related events as desired. Player->OnConnected.AddLambda([this]{ /* ... */ }); Player->OnDisconnected.AddLambda([this]{ /* ... */ }); Player->OnDegraded.AddLambda([this]{ /* ... */ }); Player->OnReconnected.AddLambda([this]{ /* ... */ }); } void APC_TutorialPlayerController::LogIn(const FString& Username) { Player->LogIn( EPragma_Account_IdProvider::UNSAFE, Username, Pragma::FPlayer::FLoggedInDelegate::CreateUObject( this, &APC_TutorialPlayerController::HandleLoggedIn)); } void APC_TutorialPlayerController::LogOut() { Player->LogOut(Pragma::FPlayer::FLoggedOutDelegate::CreateUObject( this, &APC_TutorialPlayerController::HandleLoggedOut)); } void APC_TutorialPlayerController::HandleLoggedIn(const TPragmaResult<>& Result) { if (Result.IsFailure()) { UE_LOG(LogTemp, Display, TEXT("Pragma -- Login failed: %s"), *Result.ErrorCode()); return; } UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged in.")); } void APC_TutorialPlayerController::HandleLoggedOut() { UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged out.")); }
Try it out #
- Run Pragma Engine via one of the following methods.
Once the engine has started successfully, it prints the message [main] INFO main - Pragma server startup complete
.
Compile the Unreal project from your IDE.
Open your project in the Unreal Editor.
Hook up the new
APC_TutorialPlayerController
class as the default PlayerController for your game mode and level.- 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.)
- Open the Window menu and click World Settings.
- In the World Settings panel under Game Mode click the GameMode Override dropdown and select the
GM_TutorialGameMode
option. - In the World Settings panel under Game Mode ensure the Player Controller Class is set to
PC_TutorialPlayerController
.
Save and click the Play icon at the top of the window.
Click on the game window to focus it, open the in-game console with
~
, and typeLogIn test01
. This will call ourLogIn
method with the usernametest01
, which is an account that exists by default in the platform.Open the Output Log from
Window -> Output Log
(Window -> Developer Tools -> Output Log
in Unreal 4) to confirm the login was a success. You should see a line likeLogTemp: Display: Pragma -- Logged in.
The Unsafe login method is disabled by default in production. It is ‘unsafe’ because it has no password or other security measures!
Once logged in, you can call other Pragma API calls like this: Player->*Service Name*().Action()
.