メインコンテンツまでスキップ

すべてを統合する - デバイス ID でログインする - (Unreal Engine モジュール)

Last updated on May 30, 2024

Connect the UI to let player log in with device ID

In this tutorial, you will learn how to connect the login menu widget with the login implementation we created in the previous section.

You are going to get the reference of AuthEssentialsSubsystem_Starter subsystem, set up the callback function to listen when login process is completed, set up the login credentials, and send the login request.

  1. You can Find the definition in the LoginWidget_Starter class CPP file like the code below. By default, the AuthEssentialsSubsystem_Starter subsystem is hooked to a variable called AuthSubsystem:

    void ULoginWidget_Starter::NativeConstruct()
    {
    Super::NativeConstruct();

    GameInstance = Cast<UAccelByteWarsGameInstance>(GetGameInstance());
    ensure(GameInstance);

    AuthSubsystem = GameInstance->GetSubsystem<UAuthEssentialsSubsystem_Starter>();
    ensure(AuthSubsystem);

    PromptSubsystem = GameInstance->GetSubsystem<UPromptSubsystem>();
    ensure(PromptSubsystem);

    SetLoginState(ELoginState::Default);
    OnRetryLoginDelegate.Clear();
    }
  2. Since the declaration is already made, go straight to the LoginWidget_Starter CPP file and navigate to OnLoginComplete(). This function will be the callback for when the login process is completed. This function will display main menu widget on login success or display an error message on login fails:

    void ULoginWidget_Starter::OnLoginComplete(bool bWasSuccessful, const FString& ErrorMessage)
    {
    if (bWasSuccessful)
    {
    // When login succeeds, open Main Menu widget.
    UAccelByteWarsBaseUI* BaseUIWidget = Cast<UAccelByteWarsBaseUI>(GameInstance->GetBaseUIWidget());
    ensure(BaseUIWidget);
    BaseUIWidget->PushWidgetToStack(EBaseUIStackType::Menu, MainMenuWidgetClass);
    }
    else
    {
    // When login fails, show an error message.
    Tb_FailedMessage->SetText(FText::FromString(ErrorMessage));
    SetLoginState(ELoginState::Failed);
    }
    }
  3. Now, you can start to send the login request. Still in the CPP file, navigate to OnLoginWithDeviceIdButtonClicked() and replace the existing implementation with the following:

    void ULoginWidget_Starter::OnLoginWithDeviceIdButtonClicked()
    {
    SetLoginState(ELoginState::LoggingIn);
    OnRetryLoginDelegate.AddUObject(this, &ThisClass::OnLoginWithDeviceIdButtonClicked);

    // Get player controller.
    APlayerController* PC = GetWorld()->GetFirstPlayerController();
    ensure(PC);

    // Set auth credentials and send login with Device ID request.
    // Note that login with Device ID only requires the login type.
    // It doesn't require ID/username nor token/password.
    ensure(AuthSubsystem);
    AuthSubsystem->SetAuthCredentials(EAccelByteLoginType::DeviceId, TEXT(""), TEXT(""));
    AuthSubsystem->Login(PC, FAuthOnLoginCompleteDelegate_Starter::CreateUObject(this, &ThisClass::OnLoginComplete));
    }
  4. Compile your project and make sure there are no errors.