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 #

  1. In the MyPlayerController.h header file, declare the LogIn() and LogOut() public functions, and the HandleLoggedIn() and HandleLoggedOut() 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").

  1. 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.

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.