Handle Login and Logout #
In this section, we’ll be logging in with the configured SDK using a test user. This tutorial builds off the MyPlayerController.h
and MyPlayerController.cpp
files.
Prerequisites:
Writing the code #
- In the
MyPlayerController.h
header file, declare theLogIn()
andLogOut()
public functions, and theHandleLoggedIn()
andHandleLoggedOut()
private functions:
class UNREALTUTORIAL_API AMyPlayerController : 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;
};
The LogIn
and LogOut
methods are annotated with UFUNCTION(Exec)
so that you can easily call them from the in-game console for the sake of testing. To create a login UI element, change the annotation to UFUNCTION(BlueprintCallable, Category="Pragma")
.
- Define the login/logout functions in the
MyPlayerController.cpp
source file:
void AMyPlayerController::LogIn(const FString& Username)
{
Player->LogIn(
EPragma_Account_IdProvider::UNSAFE,
Username,
Pragma::FPlayer::FLoggedInDelegate::CreateUObject(
this, &AMyPlayerController::HandleLoggedIn));
}
void AMyPlayerController::LogOut()
{
Player->LogOut(Pragma::FPlayer::FLoggedOutDelegate::CreateUObject(
this, &AMyPlayerController::HandleLoggedOut));
}
void AMyPlayerController::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 AMyPlayerController::HandleLoggedOut()
{
UE_LOG(LogTemp, Display, TEXT("Pragma -- Logged out."));
}
The LogIn
and LogOut
events simply forward to the Player object’s equivalent LogIn
and LogOut
methods, including a lambda callback that will be invoked when on completion.
In the case of LogIn
, we get back a TPragmaResult
, which is a fancy way of saying “did it work?” We can inspect the Result.IsSuccessful()
or Result.IsFailure()
to answer that question. If it errors, we can use the ErrorCode()
method to get a string to log.
Try it out! #
Ensure Pragma Engine is running.
Compile the Unreal project from your IDE.
Open your project in the Unreal Editor.
Hook up the new
MyPlayerController
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_TutorialGameModeBase
option. - In the World Settings panel under Game Mode ensure the Player Controller Class is set to
MyPlayerController
.
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.
Continue on to learn how to add authentication to your login process.