すべてを統合する - プレイヤーを検索する - (Unreal Engine モジュール)
Connect the UI to search player and send friend request
Open the
FindFriendsWidget_Starter
class header file. You will define several new functions to call the subsystem's functionality.protected:
// ...
void DisplaySelfFriendCode();
void FindFriendByDisplayName(const FString& DisplayName);
void SendFriendRequestByFriendCode(const FString& FriendCode);Open the
FindFriendsWidget_Starter
class CPP file and define theDisplaySelfFriendCode
function. This function will retrieve the player's friend code and display it on the UI.void UFindFriendsWidget_Starter::DisplaySelfFriendCode()
{
Tb_FriendCode->SetText(FText::GetEmpty());
Ws_FriendCode->SetActiveWidget(Th_FriendCodeLoader);
FriendsSubsystem->GetSelfFriendCode(
GetOwningPlayer(),
FOnGetSelfFriendCodeComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& FriendCode)
{
if (bWasSuccessful && !FriendCode.IsEmpty())
{
Tb_FriendCode->SetText(FText::FromString(FriendCode));
Ws_FriendCode->SetActiveWidget(Tb_FriendCode);
}
else
{
Ws_FriendCode->SetActiveWidget(Tb_FriendCodeEmpty);
}
}
));
}Define the
FindFriendByDisplayName
function which will trigger the subsystem'sFindFriend
and display the retrieved friend information in the UI.void UFindFriendsWidget_Starter::FindFriendByDisplayName(const FString& DisplayName)
{
ensure(FriendsSubsystem);
Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
// Try to find friend by exact display name.
FriendsSubsystem->FindFriend(
GetOwningPlayer(),
DisplayName,
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 the result of find friend.
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);
}
}
));
}Define the
SendFriendRequestByFriendCode
function which will send theSendFriendRequest
immediately. However, if it's fail, it will take the supposedly friend code and use it as a display name to search for player.void UFindFriendsWidget_Starter::SendFriendRequestByFriendCode(const FString& FriendCode)
{
// Abort if trying to friend with themself.
if (FriendCode.Equals(Tb_FriendCode->GetText().ToString()))
{
Ws_FindFriends->ErrorMessage = CANNOT_INVITE_FRIEND_SELF;
Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
return;
}
ensure(FriendsSubsystem);
Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
// Try to send friend request by friend code.
FriendsSubsystem->SendFriendRequest(
GetOwningPlayer(),
FriendCode,
FOnSendFriendRequestComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, UFriendData* FriendData, const FString& ErrorMessage)
{
Lv_FindFriends->SetUserFocus(GetOwningPlayer());
Lv_FindFriends->ClearListItems();
if (!bWasSuccessful && !FriendData)
{
/* Fallback, the player might enter display name instead of friend code.
* Try to find friend by the exact display name. */
FindFriendByDisplayName(Edt_SearchBar->GetText().ToString());
}
else
{
// Reset the status to be "searched", because the data is retrieved from the result of find friend.
FriendData->Status = EFriendStatus::Searched;
Lv_FindFriends->AddItem(FriendData);
Lv_FindFriends->RequestRefresh();
Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
}
}
));
}Still in the
FindFriendsWidget_Starter
class CPP file. Navigate toNativeOnActivated()
function, which will be called upon the widget opening, and replace it with the code below. This will trigger theDisplaySelfFriendCode
immediately upon the widget activation.void UFindFriendsWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);
Edt_SearchBar->SetText(FText::FromString(TEXT("")));
Edt_SearchBar->OnTextCommitted.AddDynamic(this, &ThisClass::OnSearchBarCommitted);
Btn_Search->OnClicked().AddWeakLambda(this, [this]()
{
OnSearchBarCommitted(Edt_SearchBar->GetText(), ETextCommit::Type::OnEnter);
});
Btn_CopyFriendCode->OnClicked().AddUObject(this, &ThisClass::CopyFriendCodeToClipboard);
// Reset widgets.
Ws_FindFriends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
Lv_FindFriends->ClearListItems();
DisplaySelfFriendCode();
}In the
FindFriendsWidget_Starter
class CPP file, navigate to theOnSearchBarCommitted()
function, and replace the current implementation with the code below. Af first, the logic will treat any given text as a friend code, if it's fail, then it will treated as display name. 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;
}
SendFriendRequestByFriendCode(Text.ToString());
}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.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.cpp