Skip to main content

Put it all together - Add friends - (Unreal Engine module)

Last updated on October 24, 2024

Connect the UI to get received requests

  1. Open the FriendRequestsWidget_Starter class CPP file, navigate to the GetFriendRequestList() function, and replace the current implementation with the code below. It will get the received friend request list using the FriendsSubsystem_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);
    }
    }
    ));
    }
  2. Replace the NativeOnActivated() function with the code below. It will bind the GetFriendRequestList function to the OnCachedFriendsDataUpdated, 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();
    }
  3. 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

  1. Open the SentFriendRequestsWidget_Starter class CPP file, navigate to the GetSentFriendRequestList() function, and replace the current implementation with the code below. It will get the sent friend request list using the FriendsSubsystem_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);
    }
    }
    ));
    }
  2. Add the following code to the NativeOnActivated(). It will bind the GetSentFriendRequestList function to the OnCachedFriendsDataUpdated, 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();
    }
  3. 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

  1. Start with accepting friend requests. Open the FriendWidgetEntry_Starter class CPP file, navigate to the OnAcceptButtonClicked function, and replace the current implementation with the following code:

    void UFriendWidgetEntry_Starter::OnAcceptButtonClicked()
    {
    ensure(CachedFriendData);
    ensure(FriendsSubsystem);

    FriendsSubsystem->AcceptFriendRequest(GetOwningPlayer(), CachedFriendData->UserId);
    }
  2. 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);
    }
  3. 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