Unreal Engine Module - Add a Friend - Put it all together
Connecting UI with Get Received Friend Requests implementation
Open the
FriendRequestsWidget_Starter
class CPP file, navigate to theGetFriendRequestList()
function, and replace the current implementation with the following code. It will get the received friend request list using theFriendsSubsystem_Starter
subsystem and then display it upon completion.void UFriendRequestsWidget_Starter::GetFriendRequestList()
{
ensure(FriendsSubsystem);
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
FriendsSubsystem->GetInboundFriendRequestList(
GetOwningPlayer(),
FOnGetInboundFriendRequestListComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, TArray<UFriendData*> FriendRequests, const FString& ErrorMessage)
{
Lv_FriendRequests->SetUserFocus(GetOwningPlayer());
Lv_FriendRequests->ClearListItems();
if (bWasSuccessful)
{
Lv_FriendRequests->SetListItems(FriendRequests);
Ws_FriendRequests->SetWidgetState(FriendRequests.IsEmpty() ?
EAccelByteWarsWidgetSwitcherState::Empty :
EAccelByteWarsWidgetSwitcherState::Not_Empty);
}
else
{
Ws_FriendRequests->ErrorMessage = FText::FromString(ErrorMessage);
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
}
}
));
}Next, add the highlighted code to the
NativeOnActivated()
. It will bind theGetFriendRequestList
function to theOnCachedFriendsDataUpdated
, updating the displayed list as soon as the actual list changes.void UFriendRequestsWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
...
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendRequestList));
GetFriendRequestList();
}Finally, in the
NativeOnDeactivated()
add the following code. Basically, it will stop listening to the received friend request list changes upon the widget is closed.void UFriendRequestsWidget_Starter::NativeOnDeactivated()
{
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}Congratulations! You have connected the widget to display the received friend request list.
Connecting UI with Get Sent Friend Requests implementation
Open the
SentFriendRequestsWidget_Starter
class CPP file, navigate to theGetSentFriendRequestList()
function, and replace the current implementation with the following code. It will get the sent friend request list using theFriendsSubsystem_Starter
subsystem and then display it upon completion.void USentFriendRequestsWidget_Starter::GetSentFriendRequestList()
{
ensure(FriendsSubsystem);
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
FriendsSubsystem->GetOutboundFriendRequestList(
GetOwningPlayer(),
FOnGetOutboundFriendRequestListComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, TArray<UFriendData*> FriendRequests, const FString& ErrorMessage)
{
Lv_FriendRequests->SetUserFocus(GetOwningPlayer());
Lv_FriendRequests->ClearListItems();
if (bWasSuccessful)
{
Lv_FriendRequests->SetListItems(FriendRequests);
Ws_FriendRequests->SetWidgetState(FriendRequests.IsEmpty() ?
EAccelByteWarsWidgetSwitcherState::Empty :
EAccelByteWarsWidgetSwitcherState::Not_Empty);
}
else
{
Ws_FriendRequests->ErrorMessage = FText::FromString(ErrorMessage);
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
}
}
));
}Next, add the following code to the
NativeOnActivated()
. It will bind theGetSentFriendRequestList
function to theOnCachedFriendsDataUpdated
, updating the displayed list as soon as the actual list changes.void USentFriendRequestsWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
...
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetSentFriendRequestList));
GetSentFriendRequestList();
}Finally, in the
NativeOnDeactivated()
add the following code. Basically, it will stop listening to the sent friend request list changes upon the widget is closed.void USentFriendRequestsWidget_Starter::NativeOnDeactivated()
{
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}Congratulations! You have connected the widget to display the sent friend request list.
Connecting UI with Accept, Reject, and Cancel Friend Requests implementation
Let's start with accepting friend request first. Open the
FriendWidgetEntry_Starter
class CPP file, navigate to theOnAcceptButtonClicked
function, and replace the current implementation with the following code.void UFriendWidgetEntry_Starter::OnAcceptButtonClicked()
{
ensure(CachedFriendData);
ensure(FriendsSubsystem);
FriendsSubsystem->AcceptFriendRequest(GetOwningPlayer(), CachedFriendData->UserId);
}Still in the same file, navigate to the
OnRejectButtonClicked
function, and replace the current implementation with the following code.void UFriendWidgetEntry_Starter::OnRejectButtonClicked()
{
ensure(CachedFriendData);
ensure(FriendsSubsystem);
FriendsSubsystem->RejectFriendRequest(GetOwningPlayer(), CachedFriendData->UserId);
}Finally, navigate to the
OnCancelButtonClicked
function, and replace the current implementation with the following code to cancel the friend requests.void UFriendWidgetEntry_Starter::OnCancelButtonClicked()
{
ensure(CachedFriendData);
ensure(FriendsSubsystem);
// Cancel friend request is the same as removing a friend.
FriendsSubsystem->CancelFriendRequest(GetOwningPlayer(), CachedFriendData->UserId);
}Congratulations! You have connected the widget to perform accept, reject, and cancel the friend request.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendRequestsWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendRequestsWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/SentFriendRequestsWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/SentFriendRequestsWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.cpp