すべてを統合する - プレイヤーを検索する - (Unreal Engine モジュール)
Connect the UI to search player and send friend request
Open the
FindFriendsWidget_Starter
class CPP file. There, you will find a function calledNativeOnActivated()
, which will be called upon the widget opening. In that function, you have already stored a reference toFriendsSubsystem_Starter
, so you can use it to call friends functionalities you created previously.In the
FindFriendsWidget_Starter
class CPP file, navigate to theOnSearchBarCommitted()
function, and replace the current implementation with the code below. It will find a potential friend using theFriendsSubsystem_Starter
subsystem. Once complete, 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);
}
}
));
}Open the
FriendWidgetEntry_Starter
class CPP file, navigate to theOnInviteButtonClicked()
, and replace the current implementation with the code below. 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);
}
}
));
}Compile your project and make sure there are no errors.
Resources
- The files used in this tutorial section are available in the Unreal Byte Wars GitHub repository.