Put it all together - Friend list - (Unreal Engine module)
Last updated on October 24, 2024
Connect the UI to get friend list
Open the
FriendsWidget_Starter
class CPP file, navigate to theGetFriendList()
function, and replace the current implementation with the code below. 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->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);
}
}
));
}Add the code below 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()
{
Super::NativeOnActivated();
Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);
// Reset widgets.
Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
Tv_Friends->ClearListItems();
Tv_Friends->OnItemClicked().AddUObject(this, &ThisClass::OnFriendEntryClicked);
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendList));
GetFriendList();
}In
NativeOnDeactivated()
, add the code below. Essentially, it will stop listening to the friend list changes upon the widget closing.void UFriendsWidget_Starter::NativeOnDeactivated()
{
Btn_Back->OnClicked().Clear();
Tv_Friends->OnItemClicked().Clear();
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}
Resources
- The files used in this tutorial section are available in the Unreal Byte Wars GitHub repository.