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

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

Last updated on May 30, 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->SetUserFocus(GetOwningPlayer());
    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()
    {
    ...

    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()
    {
    Tv_Friends->OnItemClicked().Clear();

    FriendsSubsystem->UnbindOnCachedFriendsDataUpdated(GetOwningPlayer());

    Super::NativeOnDeactivated();
    }

Resources