フレンド検索メニューを追加する - プレイヤーを検索する - (Unreal Engine モジュール)
注釈:本資料はAI技術を用いて翻訳されています。
メニューの内容
このセクションでは、表示名でフレンド候補を検索し、フレンド招待リクエストを送信するために使用するウィジェットの準備方法を学びます。ウィジェットはこのチュートリアルにまとめられており、リソースセクションで入手できます。
-
FindFriendsWidget_Starter: フレンド候補を検索するための機能を呼び出すために使用する C++ クラスです。- ヘッダーファイル:
/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.h - CPP ファイル:
/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.cpp - Blueprint ウィジェット:
/Content/TutorialModules/Social/FriendsEssentials/UI/W_FindFriends_Starter.uasset
- ヘッダーファイル:
-
FriendWidgetEntry_Starter: 検索結果のフレンド候補の情報(表示名、アバター、オプションボタンなど)を表示するために使用する C++ クラスです。- ヘッダーファイル:
/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.h - CPP ファイル:
/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.cpp - Blueprint ウィジェット:
/Content/TutorialModules/Social/FriendsEssentials/UI/W_FriendEntry_Starter.uasset
- ヘッダーファイル:
これらのウィジェットがどのように構築されているかの詳細を見てみましょう。
Find Friend ウィジェット
このウィジェットには2つの主要なコンポーネントがあります:プレイヤー検索部分とフレンドコード取得部分です。プレイヤー検索には、リクエストステータスの各状態を表すいくつかの状態があります:空、読み込み中、エラー、空でない(検索結果リストの表示)。これらの状態は、カスタム AccelByte Widget Switcher である UAccelByteWarsWidgetSwitcher を使用して実現されています。リスト自体は、エントリーウィジェットクラスを受け取り、エントリーを動的に生成する List View を使用して実装されています。同様に、プレイヤー検索は複数の状態で構成されています:デフォルト、読み込み中、コードなし。これは Widget Switcher を使用して実現されています。
このウィジェットには、検索バー、検索ボタン、コードコピーボタンも含まれています。以下は W_FindFriends_Starter Blueprint ウィジェットのプレビューです:

これらのコンポーネントは FindFriendsWidget_Starter クラスのヘッダーファイルで宣言されています:
protected:
// ...
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UWidgetSwitcher* Ws_FriendCode;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UTextBlock* Tb_FriendCode;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UTextBlock* Tb_FriendCodeEmpty;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UThrobber* Th_FriendCodeLoader;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UCommonButtonBase* Btn_CopyFriendCode;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UCommonButtonBase* Btn_Search;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UEditableText* Edt_SearchBar;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UAccelByteWarsWidgetSwitcher* Ws_FindFriends;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UListView* Lv_FindFriends;
プレイヤー検索コンポーネントの状態を変更するには Ws_FindFriends->SetWidgetState() を呼び出し、フレンドコードの状態を設定するには Ws_FriendCode->SetActiveWidget() を呼び出します。
Friend Entry ウィジェット
以下は W_FriendEntry_Starter Blueprint ウィジェットのプレビューです。このウィジェットは、表示名、アバター、オプションボタンなどのフレンド情報を表示します。これは Find Friend ウィジェットの List View のエントリーウィジェットクラスになります。

オプションボタンは Ws_OptionButtons という名前の Widget Switcher に分類されています。オプションボタンは、フレンドリクエストの送信などのアクションを実行するためのボタンです。Widget Switcher は、表示されているフレンドエントリータイプに基づいて関連するオプションボタンを表示するのに役立ちます。
List View では、エントリーウィジェットが IUserObjectListEntry インターフェースを実装する必要があります。このウィジェットの親である FriendWidgetEntry_Starter のヘッダーファイルを見ると、UAccelByteWarsWidgetEntry を継承しており、そこに IUserObjectListEntry が実装されていることがわかります。ウィジェットがエントリーウィジェットとして正しく機能するためには、NativeOnListItemObjectSet() 関数を実装する必要があります。これは List View が呼び出すセットアップ関数です。ここですべての UI セットアップを行う必要があります。これはすでに設定されているので、任意の List View に使用するか、NativeOnListItemObjectSet() 関数を直接呼び出して手動でセットアップすることができます。
void UFriendWidgetEntry_Starter::NativeOnListItemObjectSet(UObject* ListItemObject)
{
Super::NativeOnListItemObjectSet(ListItemObject);
CachedFriendData = Cast<UFriendData>(ListItemObject);
// Display display name.
if (!CachedFriendData->DisplayName.IsEmpty())
{
Tb_DisplayName->SetText(FText::FromString(CachedFriendData->DisplayName));
}
else
{
Tb_DisplayName->SetText(FText::FromString(
UTutorialModuleOnlineUtility::GetUserDefaultDisplayName(CachedFriendData->UserId.ToSharedRef().Get())));
}
// Display avatar image.
const FString AvatarURL = CachedFriendData->AvatarURL;
Img_Avatar->LoadImage(AvatarURL);
if(CachedFriendData->DataSource != EDataSource::User)
{
Ws_OptionButtons->SetVisibility(ESlateVisibility::Hidden);
if(CachedFriendData->DataSource == EDataSource::GameSessionPlayer)
{
URecentPlayersSubsystem* RecentPlayersSubsystem = GetGameInstance()->GetSubsystem<URecentPlayersSubsystem>();
if(RecentPlayersSubsystem != nullptr)
{
Tb_GameSessionStatus->SetVisibility(ESlateVisibility::Visible);
Tb_GameSessionStatus->SetText(FText::FromString(RecentPlayersSubsystem->GetGameSessionPlayerStatus(CachedFriendData)));
const FOnlineSubsystemAccelByte* Subsystem = static_cast<FOnlineSubsystemAccelByte*>(Online::GetSubsystem(GetWorld()));
IOnlineSessionPtr SessionInterface = Subsystem->GetSessionInterface();
if(SessionInterface.IsValid())
{
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
SessionInterface->ClearOnSessionParticipantJoinedDelegates(this);
SessionInterface->AddOnSessionParticipantJoinedDelegate_Handle(FOnSessionParticipantJoinedDelegate::CreateUObject(this, &ThisClass::OnSessionParticipantsJoined));
#else
SessionInterface->ClearOnSessionParticipantsChangeDelegates(this);
SessionInterface->AddOnSessionParticipantsChangeDelegate_Handle(FOnSessionParticipantsChangeDelegate::CreateUObject(this, &ThisClass::OnSessionParticipantsChanged));
#endif
}
}
}
}
else
{
// Display options based on friend's invitation status.
Ws_OptionButtons->SetActiveWidgetIndex((uint8)CachedFriendData->Status);
// Show the reason why the player cannot send invitation request.
Btn_Invite->SetVisibility(!CachedFriendData->bCannotBeInvited ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
Tb_CannotInviteMessage->SetVisibility(CachedFriendData->bCannotBeInvited ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
Tb_CannotInviteMessage->SetText(FText::FromString(CachedFriendData->ReasonCannotBeInvited));
Tb_GameSessionStatus->SetVisibility(ESlateVisibility::Collapsed);
}
OnListItemObjectSet.Broadcast();
}
このウィジェットをフレンド候補の検索結果を表示するために使用するため、上記の関数のおかげで招待ボタンが自動的に表示されます。
以下は、このウィジェットで使用されるコンポーネントの宣言です。宣言は FriendWidgetEntry_Starter クラスのヘッダーファイルにあります。
protected:
// ...
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UAccelByteWarsAsyncImageWidget* Img_Avatar;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UTextBlock* Tb_DisplayName;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UWidgetSwitcher* Ws_OptionButtons;
UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional, BlueprintProtected = true, AllowPrivateAccess = true))
UCommonButtonBase* Btn_Invite;
UI の準備
このセクションでは、このモジュールのチュートリアルに従うためにウィジェットを準備する方法を学びます。
-
FriendWidgetEntry_Starterクラスの CPP ファイルを開き、OnInviteButtonClicked()関数に移動します。この関数は、招待ボタンがクリックされたときにトリガーされます。後で、この関数でフレンド招待リクエストの送信を呼び出します。void UFriendWidgetEntry_Starter::OnInviteButtonClicked()
{
// ...
} -
FindFriendsWidget_Starterクラスの CPP ファイルを開き、OnSearchBarCommitted()関数に移動します。この関数は、W_FindFriends_Starterの検索バーにキーワードを送信したときに呼び出されます。後で、ここでフレンド候補の検索結果の表示を呼び出します。void UFindFriendsWidget_Starter::OnSearchBarCommitted(const FText& Text, ETextCommit::Type CommitMethod)
{
// ...
} -
プロジェクトをビルドし、Unreal Engine エディターで開きます。エディターで
/Content/TutorialModules/Social/FriendsEssentials/に移動します。DA_FriendsEssentialsを開き、Is Starter Mode Activeを有効にします。データアセットを再度保存します。これにより、前述のウィジェットを含むフレンド関連のウィジェットがアクティブになり、ゲームをプレイするときにそれらをナビゲートできるようになります。
-
エディターでゲームをプレイします。ログインすると、Social > Find Friends に移動できるはずです。
リソース
- このチュートリアルセクションで使用されているファイルは、Unreal Byte Wars GitHub リポジトリで入手できます。
- AccelByteWars/Content/TutorialModules/Social/FriendsEssentials/DA_FriendsEssentials.uasset
- AccelByteWars/Content/TutorialModules/Social/FriendsEssentials/UI/W_FindFriends_Starter.uasset
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FindFriendsWidget_Starter.cpp
- AccelByteWars/Content/TutorialModules/Social/FriendsEssentials/UI/W_FriendEntry_Starter.uasset
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Social/FriendsEssentials/UI/FriendWidgetEntry_Starter.cpp