Unreal Engine Module - Display a List of Friends - Put it all together
Last updated on January 13, 2024
Connecting UI with Get Friend List implementation
Open the
FriendsWidget_Starter
class CPP file, navigate to theGetFriendList()
function, and replace the current implementation with the following code. It will get the friend list using theFriendsSubsystem_Starter
and then display it upon completion.void UFriendsWidget_Starter::GetFriendList()
{
ensure(FriendsSubsystem);
Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
FriendsSubsystem->GetFriendList(
GetOwningPlayer(),
FOnGetFriendListComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, TArray<UFriendData*> Friends, const FString& ErrorMessage)
{
Tv_Friends->SetUserFocus(GetOwningPlayer());
Tv_Friends->ClearListItems();
if (bWasSuccessful)
{
Tv_Friends->SetListItems(Friends);
Ws_Friends->SetWidgetState(Friends.IsEmpty() ?
EAccelByteWarsWidgetSwitcherState::Empty :
EAccelByteWarsWidgetSwitcherState::Not_Empty);
}
else
{
Ws_Friends->ErrorMessage = FText::FromString(ErrorMessage);
Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
}
}
));
}Next, add the following code to the
NativeOnActivated()
. It will bind theGetFriendList
function to theOnCachedFriendsDataUpdated
, updating the displayed list as soon as the actual list changes.void UFriendsWidget_Starter::NativeOnActivated()
{
...
Tv_Friends->OnItemClicked().AddUObject(this, &ThisClass::OnFriendEntryClicked);
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendList));
GetFriendList();
}Finally, in the
NativeOnDeactivated()
add the following code. Basically, it will stop listening to the friend list changes upon the widget is closed.void UFriendsWidget_Starter::NativeOnDeactivated()
{
Tv_Friends->OnItemClicked().Clear();
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}Congratulations! You have connected the widget to display the friend list.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.