Unreal Engine Module - Display a List of Friends - Use the Online Subsystem to query friend list
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.
In the previous tutorial, you have created a function namely
CacheFriendList()
in theFriendsSubsystem_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.Open the
FriendsSubsystem_Starter
class header file and create the following function declarations.public:
void GetFriendList(const APlayerController* PC, const FOnGetFriendListComplete& OnComplete = FOnGetFriendListComplete());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);
}
}));
}Congratulations! Your get friend list implementation is completed.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.