Skip to main content

Unreal Engine Module - Search Player - Put it all together

Last updated on January 13, 2024

Connecting UI with Find Friends implementation

  1. First, open the FindFriendsWidget_Starter class CPP file. There, you will find a function called NativeOnActivated(), which will be called upon the widget is opened. In that function, we have already stored a reference to FriendsSubsystem_Starter, so you can use it to call friends functionalities that you have created in the previous section.

  2. Next, in the FindFriendsWidget_Starter class CPP file, navigate to the OnSearchBarCommitted() function, and replace the current implementation with the following code. It will find a potential friend using the FriendsSubsystem_Starter subsystem. Once completed, it will show the result.

    void UFindFriendsWidget_Starter::OnSearchBarCommitted(const FText& Text, ETextCommit::Type CommitMethod)
    {
    if (CommitMethod != ETextCommit::Type::OnEnter || Text.IsEmpty())
    {
    return;
    }

    ensure(FriendsSubsystem);

    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    FriendsSubsystem->FindFriend(
    GetOwningPlayer(),
    Text.ToString(),
    FOnFindFriendComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
    {
    Lv_FindFriends->SetUserFocus(GetOwningPlayer());
    Lv_FindFriends->ClearListItems();

    if (bWasSuccessful)
    {
    // Reset the status to be "searched", because the data is retrieved from find friend result.
    FriendData->Status = EFriendStatus::Searched;
    Lv_FindFriends->AddItem(FriendData);
    Lv_FindFriends->RequestRefresh();
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    else
    {
    Ws_FindFriends->ErrorMessage = FText::FromString(ErrorMessage);
    Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    }
    }
    ));
    }
  3. Next, open the FriendWidgetEntry_Starter class CPP file, navigate to the OnInviteButtonClicked(), and replace the current implementation with the following code. It will send a friend invitation request using the FriendsSubsystem_Starter subsystem.

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

    FriendsSubsystem->SendFriendRequest(
    GetOwningPlayer(),
    CachedFriendData->UserId,
    FOnSendFriendRequestComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
    {
    if (bWasSuccessful)
    {
    // Since the invitation is already sent, refresh the entry data to show that the friend cannot be invited again.
    CachedFriendData->bCannotBeInvited = FriendData->bCannotBeInvited;
    CachedFriendData->ReasonCannotBeInvited = FriendData->ReasonCannotBeInvited;
    NativeOnListItemObjectSet(CachedFriendData);
    }
    }
    ));
    }
  4. Build the project and open it in the Unreal Engine Editor. In the Unreal Engine Editor, play the game. In the Module: Login with Steam, you have implemented login with Steam. Log in with the Device ID and then test to find a potential friend by inputting your Steam account display name. Then, send the friend invitation.

    Test to find and send a friend request

  5. When the friend invitation is successful, you should also see the log below.

    LogFriendsEssentials: Warning: Success to send a friend request.
  6. Congratulations! You have connected the widgets with the subsystem, perform find user, and send a friend invitation request.

Resources