Unreal Engine Module - Search Player - Put it all together
Connecting UI with Find Friends implementation
First, open the
FindFriendsWidget_Starter
class CPP file. There, you will find a function calledNativeOnActivated()
, which will be called upon the widget is opened. In that function, we have already stored a reference toFriendsSubsystem_Starter
, so you can use it to call friends functionalities that you have created in the previous section.Next, in the
FindFriendsWidget_Starter
class CPP file, navigate to theOnSearchBarCommitted()
function, and replace the current implementation with the following code. It will find a potential friend using theFriendsSubsystem_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);
}
}
));
}Next, open the
FriendWidgetEntry_Starter
class CPP file, navigate to theOnInviteButtonClicked()
, and replace the current implementation with the following code. It will send a friend invitation request using theFriendsSubsystem_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);
}
}
));
}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.
When the friend invitation is successful, you should also see the log below.
LogFriendsEssentials: Warning: Success to send a friend request.
Congratulations! You have connected the widgets with the subsystem, perform find user, and send a friend invitation request.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.