Skip to main content

Unreal Engine Module - Manage Friends - Put it all together

Last updated on January 13, 2024

Connecting UI with Get Blocked Players Implementation

  1. Open the BlockedPlayersWidget_Starter class CPP file, navigate to the GetBlockedPlayerList() function, and replace the current implementation with the following code. It will get the blocked player list using the ManagingFriendsSubsystem_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);
    }
    }
    ));
    }
  2. Next, add the following code to the NativeOnActivated(). It will bind the GetBlockedPlayerList function to the OnCachedBlockedPlayersDataUpdated, updating the displayed list as soon as the actual list changes.

    void UBlockedPlayersWidget_Starter::NativeOnActivated()
    {
    ...

    ManagingFriendsSubsystem->BindOnCachedBlockedPlayersDataUpdated(GetOwningPlayer(), FOnGetCacheBlockedPlayersDataUpdated::CreateUObject(this, &ThisClass::GetBlockedPlayerList));
    GetBlockedPlayerList();
    }
  3. 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();
    }
  4. Congratulations! You have connected the widget to display the blocked player list.

Connecting UI with Unfriend, Block, and Unblock Player

  1. 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);
    }
  2. 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);
    }
  3. Next, open the BlockedPlayerWidgetEntry_Starter class CPP file. Replace the existing implementation for OnUnblockButtonClicked with the following to unblock a player.

    void UBlockedPlayerWidgetEntry_Starter::OnUnblockButtonClicked()
    {
    ensure(CachedBlockedPlayerData);
    ensure(ManagingFriendsSubsystem);

    ManagingFriendsSubsystem->UnblockPlayer(GetOwningPlayer(), CachedBlockedPlayerData->UserId);
    }
  4. Congratulations! You have connected the widget to perform unfriend, block, and unblock a player.

Resources