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

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

Last updated on May 30, 2024

Connect the UI to search player and send friend request

  1. Open the FindFriendsWidget_Starter class CPP file. There, you will find a function called NativeOnActivated(), which will be called upon the widget opening. In that function, you have already stored a reference to FriendsSubsystem_Starter, so you can use it to call friends functionalities you created previously.

  2. In the FindFriendsWidget_Starter class CPP file, navigate to the OnSearchBarCommitted() function, and replace the current implementation with the code below. It will find a potential friend using the FriendsSubsystem_Starter subsystem. 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;
    }

    ensure(FriendsSubsystem);

    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    FriendsSubsystem->FindFriend(
    GetOwningPlayer(),
    Text.ToString(),
    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 find friend result.
    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);
    }
    }
    ));
    }
  3. 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);
    }
    }
    ));
    }
  4. Compile your project and make sure there are no errors.

Resources