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

すべてを統合する - プレイヤーを検索する - (Unreal Engine モジュール)

Last updated on October 23, 2024

Connect the UI to search player and send friend request

  1. Open the FindFriendsWidget_Starter class header file. You will define several new functions to call the subsystem's functionality.

    protected:
    // ...
    void DisplaySelfFriendCode();

    void FindFriendByDisplayName(const FString& DisplayName);
    void SendFriendRequestByFriendCode(const FString& FriendCode);
  2. Open the FindFriendsWidget_Starter class CPP file and define the DisplaySelfFriendCode function. This function will retrieve the player's friend code and display it on the UI.

    void UFindFriendsWidget_Starter::DisplaySelfFriendCode()
    {
    Tb_FriendCode->SetText(FText::GetEmpty());
    Ws_FriendCode->SetActiveWidget(Th_FriendCodeLoader);

    FriendsSubsystem->GetSelfFriendCode(
    GetOwningPlayer(),
    FOnGetSelfFriendCodeComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& FriendCode)
    {
    if (bWasSuccessful && !FriendCode.IsEmpty())
    {
    Tb_FriendCode->SetText(FText::FromString(FriendCode));
    Ws_FriendCode->SetActiveWidget(Tb_FriendCode);
    }
    else
    {
    Ws_FriendCode->SetActiveWidget(Tb_FriendCodeEmpty);
    }
    }
    ));
    }
  3. Define the FindFriendByDisplayName function which will trigger the subsystem's FindFriend and display the retrieved friend information in the UI.

    void UFindFriendsWidget_Starter::FindFriendByDisplayName(const FString& DisplayName)
    {
    ensure(FriendsSubsystem);

    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    // Try to find friend by exact display name.
    FriendsSubsystem->FindFriend(
    GetOwningPlayer(),
    DisplayName,
    FOnFindFriendComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
    {
    Lv_FindFriends->SetUserFocus(GetOwningPlayer());
    Lv_FindFriends->ClearListItems();

    if (bWasSuccessful)
    {
    // Reset the status to be "searched", because the data is retrieved from the result of find friend.
    FriendData->Status = EFriendStatus::Searched;
    Lv_FindFriends->AddItem(FriendData);
    Lv_FindFriends->RequestRefresh();
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    else
    {
    Ws_FindFriends->ErrorMessage = FText::FromString(ErrorMessage);
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    }
    }
    ));
    }
  4. Define the SendFriendRequestByFriendCode function which will send the SendFriendRequest immediately. However, if it's fail, it will take the supposedly friend code and use it as a display name to search for player.

    void UFindFriendsWidget_Starter::SendFriendRequestByFriendCode(const FString& FriendCode)
    {
    // Abort if trying to friend with themself.
    if (FriendCode.Equals(Tb_FriendCode->GetText().ToString()))
    {
    Ws_FindFriends->ErrorMessage = CANNOT_INVITE_FRIEND_SELF;
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    return;
    }

    ensure(FriendsSubsystem);

    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    // Try to send friend request by friend code.
    FriendsSubsystem->SendFriendRequest(
    GetOwningPlayer(),
    FriendCode,
    FOnSendFriendRequestComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
    {
    Lv_FindFriends->SetUserFocus(GetOwningPlayer());
    Lv_FindFriends->ClearListItems();

    if (!bWasSuccessful && !FriendData)
    {
    /* Fallback, the player might enter display name instead of friend code.
    * Try to find friend by the exact display name. */
    FindFriendByDisplayName(Edt_SearchBar->GetText().ToString());
    }
    else
    {
    // Reset the status to be "searched", because the data is retrieved from the result of find friend.
    FriendData->Status = EFriendStatus::Searched;
    Lv_FindFriends->AddItem(FriendData);
    Lv_FindFriends->RequestRefresh();
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    }
    ));
    }
  5. Still in the FindFriendsWidget_Starter class CPP file. Navigate to NativeOnActivated() function, which will be called upon the widget opening, and replace it with the code below. This will trigger the DisplaySelfFriendCode immediately upon the widget activation.

    void UFindFriendsWidget_Starter::NativeOnActivated()
    {
    Super::NativeOnActivated();

    Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);

    Edt_SearchBar->SetText(FText::FromString(TEXT("")));
    Edt_SearchBar->OnTextCommitted.AddDynamic(this, &ThisClass::OnSearchBarCommitted);
    Btn_Search->OnClicked().AddWeakLambda(this, [this]()
    {
    OnSearchBarCommitted(Edt_SearchBar->GetText(), ETextCommit::Type::OnEnter);
    });
    Btn_CopyFriendCode->OnClicked().AddUObject(this, &ThisClass::CopyFriendCodeToClipboard);

    // Reset widgets.
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    Lv_FindFriends->ClearListItems();

    DisplaySelfFriendCode();
    }
  6. In the FindFriendsWidget_Starter class CPP file, navigate to the OnSearchBarCommitted() function, and replace the current implementation with the code below. Af first, the logic will treat any given text as a friend code, if it's fail, then it will treated as display name. Once complete, it will show the result.

    void UFindFriendsWidget_Starter::OnSearchBarCommitted(const FText& Text, ETextCommit::Type CommitMethod)
    {
    if (CommitMethod != ETextCommit::Type::OnEnter || Text.IsEmpty())
    {
    return;
    }

    SendFriendRequestByFriendCode(Text.ToString());
    }
  7. Open the FriendWidgetEntry_Starter class CPP file, navigate to the OnInviteButtonClicked(), and replace the current implementation with the code below. It will send a friend invitation request using the FriendsSubsystem_Starter subsystem.

    void UFriendWidgetEntry_Starter::OnInviteButtonClicked()
    {
    ensure(CachedFriendData);
    ensure(FriendsSubsystem);

    FriendsSubsystem->SendFriendRequest(
    GetOwningPlayer(),
    CachedFriendData->UserId,
    FOnSendFriendRequestComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
    {
    if (bWasSuccessful)
    {
    // Since the invitation is already sent, refresh the entry data to show that the friend cannot be invited again.
    CachedFriendData->bCannotBeInvited = FriendData->bCannotBeInvited;
    CachedFriendData->ReasonCannotBeInvited = FriendData->ReasonCannotBeInvited;
    NativeOnListItemObjectSet(CachedFriendData);
    }
    }
    ));
    }
  8. Compile your project and make sure there are no errors.

Resources