すべてを統合する - フレンドを追加する - (Unreal Engine モジュール)
Connect the UI to get received requests
Open the
FriendRequestsWidget_Starter
class CPP file, navigate to theGetFriendRequestList()
function, and replace the current implementation with the code below. 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->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);
}
}
));
}Replace the
NativeOnActivated()
function with the code below. It will bind theGetFriendRequestList
function to theOnCachedFriendsDataUpdated
, updating the displayed list as soon as the actual list changes.void UFriendRequestsWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);
// Reset widgets.
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
Lv_FriendRequests->ClearListItems();
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendRequestList));
GetFriendRequestList();
}In the
NativeOnDeactivated()
, add the following code. Essentially, it will stop listening to the received friend request list changes upon the widget closing.void UFriendRequestsWidget_Starter::NativeOnDeactivated()
{
Btn_Back->OnClicked().Clear();
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}
Connect the UI to get sent requests
Open the
SentFriendRequestsWidget_Starter
class CPP file, navigate to theGetSentFriendRequestList()
function, and replace the current implementation with the code below. 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->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);
}
}
));
}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();
Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);
// Reset widgets.
Ws_FriendRequests->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
Lv_FriendRequests->ClearListItems();
FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetSentFriendRequestList));
GetSentFriendRequestList();
}In
NativeOnDeactivated()
, add the code below. Essentially, it will stop listening to the sent friend request list changes upon the widget closing.void USentFriendRequestsWidget_Starter::NativeOnDeactivated()
{
Btn_Back->OnClicked().Clear();
FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}
Connect the UI to accept, reject, and cancel requests
Start with accepting friend requests. 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);
}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);
}
Resources
- The files used in this tutorial section are available in the Unreal 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