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 #

  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.

Try it out! #

  1. Ensure Pragma Engine is running.

  2. Compile the Unreal project from your IDE.

  3. Open your project in the Unreal Editor.

  4. Hook up the new MyPlayerController class as the default PlayerController for your game mode and level.

    1. 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.)
    2. Open the Window menu and click World Settings.
    3. In the World Settings panel under Game Mode click the GameMode Override dropdown and select the GM_TutorialGameModeBase option.
    4. In the World Settings panel under Game Mode ensure the Player Controller Class is set to MyPlayerController.

  5. Save and click the Play icon at the top of the window.

  6. Click on the game window to focus it, open the in-game console with ~, and type LogIn test01. This will call our LogIn method with the username test01, which is an account that exists by default in the platform.

  7. 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 like LogTemp: 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.