Unreal Engine Module - Manage Friends - Put it all together
Connecting UI with Get Blocked Players Implementation
Open the
BlockedPlayersWidget_Starter
class CPP file, navigate to theGetBlockedPlayerList()
function, and replace the current implementation with the following code. It will get the blocked player list using theManagingFriendsSubsystem_Starter
subsystem and then display it upon completion.void UBlockedPlayersWidget_Starter::GetBlockedPlayerList()
{
ensure(ManagingFriendsSubsystem);
Ws_BlockedPlayers->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
ManagingFriendsSubsystem->GetBlockedPlayerList(
GetOwningPlayer(),
FOnGetBlockedPlayerListComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, TArray<UFriendData*> BlockedPlayers, const FString& ErrorMessage)
{
Lv_BlockedPlayers->SetUserFocus(GetOwningPlayer());
Lv_BlockedPlayers->ClearListItems();
if (bWasSuccessful)
{
Lv_BlockedPlayers->SetListItems(BlockedPlayers);
Ws_BlockedPlayers->SetWidgetState(BlockedPlayers.IsEmpty() ?
EAccelByteWarsWidgetSwitcherState::Empty :
EAccelByteWarsWidgetSwitcherState::Not_Empty);
}
else
{
Ws_BlockedPlayers->ErrorMessage = FText::FromString(ErrorMessage);
Ws_BlockedPlayers->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
}
}
));
}Next, add the following code to the
NativeOnActivated()
. It will bind theGetBlockedPlayerList
function to theOnCachedBlockedPlayersDataUpdated
, updating the displayed list as soon as the actual list changes.void UBlockedPlayersWidget_Starter::NativeOnActivated()
{
...
ManagingFriendsSubsystem->BindOnCachedBlockedPlayersDataUpdated(GetOwningPlayer(), FOnGetCacheBlockedPlayersDataUpdated::CreateUObject(this, &ThisClass::GetBlockedPlayerList));
GetBlockedPlayerList();
}Finally, in the
NativeOnDeactivated()
add the following code. Basically, it will stop listening to the blocked player list changes upon the widget is closed.void UBlockedPlayersWidget_Starter::NativeOnDeactivated()
{
ManagingFriendsSubsystem->UnbindOnCachedBlockedPlayersDataUpdated(GetOwningPlayer());
Super::NativeOnDeactivated();
}Congratulations! You have connected the widget to display the blocked player list.
Connecting UI with Unfriend, Block, and Unblock Player
Let's start with unfriend a friend first. Open the
ManagingFriendsSubsystem_Starter
class CPP file. In the following function, replace the existing implementation with the following.void UManagingFriendsSubsystem_Starter::OnUnfriendButtonClicked(const APlayerController* PC, const FUniqueNetIdRepl FriendUserId, const FOnUnfriendComplete& OnComplete)
{
Unfriend(PC, FriendUserId, OnComplete);
}Still in the same file, replace the existing implementation for
OnBlockButtonClicked
with the following to block a player.void UManagingFriendsSubsystem_Starter::OnBlockButtonClicked(const APlayerController* PC, const FUniqueNetIdRepl BlockedPlayerUserId, const FOnBlockPlayerComplete& OnComplete)
{
BlockPlayer(PC, BlockedPlayerUserId, OnComplete);
}Next, open the
BlockedPlayerWidgetEntry_Starter
class CPP file. Replace the existing implementation forOnUnblockButtonClicked
with the following to unblock a player.void UBlockedPlayerWidgetEntry_Starter::OnUnblockButtonClicked()
{
ensure(CachedBlockedPlayerData);
ensure(ManagingFriendsSubsystem);
ManagingFriendsSubsystem->UnblockPlayer(GetOwningPlayer(), CachedBlockedPlayerData->UserId);
}Congratulations! You have connected the widget to perform unfriend, block, and unblock a player.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/ManagingFriends/UI/BlockedPlayersWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/ManagingFriends/UI/BlockedPlayersWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/ManagingFriends/UI/BlockedPlayerWidgetEntry_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/ManagingFriends/UI/BlockedPlayerWidgetEntry_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/ManagingFriends/ManagingFriendsSubsystem_Starter.cpp