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

Unreal Engine Module - Display a List of Friends - Use the Online Subsystem to query friend list

Last updated on January 13, 2024

What's in the Starter Pack

In the previous module, you implemented some friend functionalities in the FriendSubsystem_Starter subsystem class. You will continue to use that class to follow this section.

Before you start, we have prepared several delegates in the /Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/FriendsEssentialsModels.h file that you will use along with this tutorial.

  • Delegates to be used as callback when getting friend list completed.

    DECLARE_DELEGATE_ThreeParams(FOnGetFriendListComplete, bool /*bWasSuccessful*/, TArray<UFriendData*> /*Friends*/, const FString& /*ErrorMessage*/);

Implement Get Friend List

In this section, you will implement functionalities to get the friend list.

  1. In the previous tutorial, you have created a function namely CacheFriendList() in the FriendsSubsystem_Starter that is used to get and cache the friend list. You need to filter that cached friend list to get the friend list that has already been accepted only.

  2. Open the FriendsSubsystem_Starter class header file and create the following function declarations.

    public:
    void GetFriendList(const APlayerController* PC, const FOnGetFriendListComplete& OnComplete = FOnGetFriendListComplete());
  3. Next, let's create the definition for the function above. Open the FriendsSubsystem_Starter class CPP file and add the following code. It will filter the cached friend list to get the accepted friend list only.

    void UFriendsSubsystem_Starter::GetFriendList(const APlayerController* PC, const FOnGetFriendListComplete& OnComplete)
    {
    if (!ensure(FriendsInterface))
    {
    UE_LOG_FRIENDS_ESSENTIALS(Warning, TEXT("Cannot get friend list. Friends Interface is not valid."));
    return;
    }

    // Get accepted friend list from cache.
    GetCacheFriendList(PC, FOnGetCacheFriendListComplete::CreateWeakLambda(this, [this, OnComplete](bool bWasSuccessful, TArray<TSharedRef<FOnlineFriend>>& CachedFriendList, const FString& ErrorMessage)
    {
    if (bWasSuccessful)
    {
    // Filter accepted friends.
    CachedFriendList = CachedFriendList.FilterByPredicate([](const TSharedRef<FOnlineFriend>& Friend)
    {
    return Friend->GetInviteStatus() == EInviteStatus::Accepted;
    });

    TArray<UFriendData*> AcceptedFriendList;
    for (const TSharedRef<FOnlineFriend>& TempData : CachedFriendList)
    {
    AcceptedFriendList.Add(UFriendData::ConvertToFriendData(TempData));
    }

    OnComplete.ExecuteIfBound(true, AcceptedFriendList, TEXT(""));
    }
    else
    {
    OnComplete.ExecuteIfBound(false, TArray<UFriendData*>(), ErrorMessage);
    }
    }));
    }
  4. Congratulations! Your get friend list implementation is completed.

Resources