メインコンテンツまでスキップ

すべてを統合する - フレンドリスト - (Unreal Engine モジュール)

Last updated on October 23, 2024

Connect the UI to get friend list

  1. Open the FriendsWidget_Starter class CPP file, navigate to the GetFriendList() function, and replace the current implementation with the code below. It will get the friend list using the FriendsSubsystem_Starter and then display it upon completion.

    void UFriendsWidget_Starter::GetFriendList()
    {
    ensure(FriendsSubsystem);

    Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    FriendsSubsystem->GetFriendList(
    GetOwningPlayer(),
    FOnGetFriendListComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, TArray<UFriendData*> Friends, const FString& ErrorMessage)
    {
    Tv_Friends->ClearListItems();

    if (bWasSuccessful)
    {
    Tv_Friends->SetListItems(Friends);
    Ws_Friends->SetWidgetState(Friends.IsEmpty() ?
    EAccelByteWarsWidgetSwitcherState::Empty :
    EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    else
    {
    Ws_Friends->ErrorMessage = FText::FromString(ErrorMessage);
    Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    }
    }
    ));
    }
  2. Add the code below to the NativeOnActivated(). It will bind the GetFriendList function to the OnCachedFriendsDataUpdated, updating the displayed list as soon as the actual list changes.

    void UFriendsWidget_Starter::NativeOnActivated()
    {
    Super::NativeOnActivated();

    Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);

    // Reset widgets.
    Ws_Friends->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    Tv_Friends->ClearListItems();

    Tv_Friends->OnItemClicked().AddUObject(this, &ThisClass::OnFriendEntryClicked);

    FriendsSubsystem->BindOnCachedFriendsDataUpdated(GetOwningPlayer(), FOnCachedFriendsDataUpdated::CreateUObject(this, &ThisClass::GetFriendList));
    GetFriendList();
    }
  3. In NativeOnDeactivated(), add the code below. Essentially, it will stop listening to the friend list changes upon the widget closing.

    void UFriendsWidget_Starter::NativeOnDeactivated()
    {
    Btn_Back->OnClicked().Clear();

    Tv_Friends->OnItemClicked().Clear();

    FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());

    Super::NativeOnDeactivated();
    }

Resources