Handle Login and Logout #
In this section, we’ll be logging in with the configured SDK using a test user. Pragma Engine maintains default test users test01-test20
, by default in the platform.
This tutorial builds off the MyPlayerController.h
and MyPlayerController.cpp
files we created in Create Player Controller and Blueprint.
Prerequisites:
Implement LogIn and LogOut functions #
Goal #
Implement a LogIn()
and LogOut()
function to allow a player to log in and out of the system.
Steps #
In the
MyPlayerController.h
header file, declare theLogIn()
andLogOut()
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: // 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. If you want to create a login UI element as well, add the BlueprintCallable annotation (UFUNCTION(Exec, 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::CreateWeakLambda( this, [=](const TPragmaResult<>& Result) { if (Result.IsSuccessful()) { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Logged in.")); } else { UE_LOG(LogTemp, Error, TEXT("MyGameClient -- Login failed: %s"), *Result.GetErrorAsString()); } })); } void AMyPlayerController::LogOut() { Player->LogOut( Pragma::FPlayer::FLoggedOutDelegate::CreateWeakLambda( this, [=] { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- 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.
Test #
To test this functionality using the Unreal in-game console, call LogIn test01
. This will call our LogIn()
function with the username test01
, which is an account that exists by default in the platform. You should see “Pragma – Logged in.” in your Unreal output log.
To apply this functionality using Unreal Blueprints, create a button that calls your LogIn()
or LogOut()
function when clicked, as well as an input field where a player can enter their username.
The Unsafe login method is disabled by default in production. It is ‘unsafe’ because it has no password or other security measures.