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

OSS でフレンドリストのクエリを実行する - フレンドリスト - (Unreal Engine モジュール)

Last updated on October 23, 2024

What's in the Starter Pack

You previously implemented some friend functionalities in the FriendSubsystem_Starter subsystem class. You will continue to use that class to follow this tutorial.

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

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

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

Implement getting friend list

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

You have already created a function named 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 only the list of players that accepted a friend request.

  1. Open the FriendsSubsystem_Starter class Header file and create the following function declarations.

    public:
    // ...
    void GetFriendList(const APlayerController* PC, const FOnGetFriendListComplete& OnComplete = FOnGetFriendListComplete());
  2. Create the definition for the function above. Open the FriendsSubsystem_Starter class CPP file and add the code below. 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.
    const int32 LocalUserNum = GetLocalUserNumFromPlayerController(PC);
    GetCacheFriendList(LocalUserNum, 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);
    }
    }));
    }

Resources