メインコンテンツまでスキップ

Unreal Engine Module - Add a Friend - Put it all together

Last updated on January 13, 2024

Connecting UI with Get Received Friend Requests implementation

  1. Open the FriendRequestsWidget_Starter class CPP file, navigate to the GetFriendRequestList() function, and replace the current implementation with the following code. 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->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);
    }
    }
    ));
    }
  2. Next, add the highlighted code to the NativeOnActivated(). 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();

    ...

    FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendRequestList));
    GetFriendRequestList();
    }
  3. 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();
    }
  4. Congratulations! You have connected the widget to display the received friend request list.

Connecting UI with Get Sent Friend Requests implementation

  1. Open the SentFriendRequestsWidget_Starter class CPP file, navigate to the GetSentFriendRequestList() function, and replace the current implementation with the following code. 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->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);
    }
    }
    ));
    }
  2. Next, 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();

    ...

    FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetSentFriendRequestList));
    GetSentFriendRequestList();
    }
  3. 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();
    }
  4. Congratulations! You have connected the widget to display the sent friend request list.

Connecting UI with Accept, Reject, and Cancel Friend Requests implementation

  1. Let's start with accepting friend request first. 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. 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);
    }
  4. Congratulations! You have connected the widget to perform accept, reject, and cancel the friend request.

Resources