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

すべてを統合する - パーティ入門 - (Unreal Engine モジュール)

Last updated on May 30, 2024

Connect the UI to display, leave, send invite, kick, and promote party member

  1. Open the PartyWidgetEntry_Starter class CPP file and add the code below to the SetPartyMember() function. This will display the party member display name and avatar.

    void UPartyWidgetEntry_Starter::SetPartyMember(const FUserOnlineAccountAccelByte& PartyMember, const bool bIsLeader)
    {
    // Display party member information.
    Ws_PartyMemberState->SetActiveWidget(W_PartyMemberPanel);

    // Set display name.
    if (PartyMember.GetDisplayName().IsEmpty())
    {
    const FUniqueNetIdAccelByteUserRef MemberABId = StaticCastSharedRef<const FUniqueNetIdAccelByteUser>(PartyMember.GetUserId());
    W_PartyMember->SetUsername(FText::FromString(UTutorialModuleOnlineUtility::GetUserDefaultDisplayName(MemberABId.Get())));
    }
    else
    {
    W_PartyMember->SetUsername(FText::FromString(PartyMember.GetDisplayName()));
    }

    // Set avatar
    FString AvatarURL;
    PartyMember.GetUserAttribute(ACCELBYTE_ACCOUNT_GAME_AVATAR_URL, AvatarURL);
    W_PartyMember->SetAvatar(AvatarURL);

    // Set color tint.
    const FLinearColor MemberColor = bIsLeader ? PartyLeaderColor : PartyMemberColor;
    SetPartyMemberColor(MemberColor);
    W_PartyMember->SetTextColor(MemberColor);
    W_PartyMember->SetAvatarTint(AvatarURL.IsEmpty() ? MemberColor : FLinearColor::White);

    // Set cached party member data as friend data.
    // Will be used to open player action menu widget.
    if (!CachedFriendData)
    {
    CachedFriendData = NewObject<UFriendData>();
    }
    CachedFriendData->UserId = PartyMember.GetUserId();
    CachedFriendData->DisplayName = PartyMember.GetDisplayName();
    CachedFriendData->AvatarURL = AvatarURL;
    }
  2. Next, open the PartyWidget_Starter class CPP file and add the code below to the DisplayParty() function. This will query the party member information and display them.

    void UPartyWidget_Starter::DisplayParty()
    {
    // Not in party.
    if (!PartyOnlineSession->IsInParty(PartyOnlineSession->GetLocalPlayerUniqueNetId(GetOwningPlayer())))
    {
    Ws_Party->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    return;
    }

    Btn_Leave->SetVisibility(ESlateVisibility::Collapsed);
    Ws_Party->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    // Query and display party member information.
    PartyOnlineSession->QueryUserInfo(
    PartyOnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
    PartyOnlineSession->GetPartyMembers(),
    FOnQueryUsersInfoComplete::CreateWeakLambda(this, [this]
    (const bool bSucceeded, const TArray<FUserOnlineAccountAccelByte*>& UsersInfo)
    {
    // If party members are changed during query, requery the party members information.
    if (UsersInfo.Num() != PartyOnlineSession->GetPartyMembers().Num())
    {
    DisplayParty();
    return;
    }
    for (int32 i = 0; i < UsersInfo.Num(); i++)
    {
    if (!UsersInfo[i]->GetUserId()->IsValid() ||
    PartyOnlineSession->GetPartyMembers()[i].Get() != UsersInfo[i]->GetUserId().Get())
    {
    DisplayParty();
    return;
    }
    }

    // Request failed.
    if (!bSucceeded)
    {
    Ws_Party->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    return;
    }

    // Clean up last party member list.
    Ws_Party->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
    Hb_Party->ClearChildren();
    Btn_Leave->SetVisibility(ESlateVisibility::Visible);

    // Display party members.
    for (int32 i = 0; i < MaxPartyMembers; i++)
    {
    if (i < UsersInfo.Num() && !UsersInfo[i])
    {
    continue;
    }

    const TWeakObjectPtr<UPartyWidgetEntry> PartyMemberEntry =
    MakeWeakObjectPtr<UPartyWidgetEntry>(CreateWidget<UPartyWidgetEntry>(this, PartyWidgetEntryClass.Get()));
    Hb_Party->AddChild(PartyMemberEntry.Get());

    // Display party member information.
    if (i < UsersInfo.Num())
    {
    PartyMemberEntry->SetPartyMember(*UsersInfo[i], PartyOnlineSession->IsPartyLeader(UsersInfo[i]->GetUserId()));
    }
    // Display button to add more party member.
    else
    {
    PartyMemberEntry->ResetPartyMember();
    }
    }
    }
    ));
    }
  3. In the same file, add the code below to the OnLeaveButtonClicked() function. This will trigger to leave the party when the leave button is clicked.

    void UPartyWidget_Starter::OnLeaveButtonClicked()
    {
    // Leave current party.
    PartyOnlineSession->LeaveParty(PartyOnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()));
    DeactivateWidget();
    }
  4. In the same file, add the code below to the NativeOnActivated() function. This function will listen to the party event to update the displayed party member on the UI.

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

    Btn_Leave->OnClicked().AddUObject(this, &ThisClass::OnLeaveButtonClicked);

    // Update the displayed party members on any changes.
    PartyOnlineSession->GetOnCreatePartyCompleteDelegates()->AddWeakLambda(this, [this](FName SessionName, bool bWasSuccessful)
    {
    DisplayParty();
    });
    PartyOnlineSession->GetOnPartyMembersChangeDelegates()->AddWeakLambda(this, [this](FName SessionName, const FUniqueNetId& Participant, bool bJoined)
    {
    DisplayParty();
    });
    PartyOnlineSession->GetOnPartySessionUpdateReceivedDelegates()->AddWeakLambda(this, [this](FName SessionName)
    {
    DisplayParty();
    });

    DisplayParty();
    }
  5. In the same file, add the code below to the NativeOnDeactivated() function. This unbinds the party events after the party widget is deactivated.

    void UPartyWidget_Starter::NativeOnDeactivated()
    {
    Btn_Leave->OnClicked().Clear();

    // Clear online delegates.
    PartyOnlineSession->GetOnCreatePartyCompleteDelegates()->RemoveAll(this);
    PartyOnlineSession->GetOnPartyMembersChangeDelegates()->RemoveAll(this);
    PartyOnlineSession->GetOnPartySessionUpdateReceivedDelegates()->RemoveAll(this);

    Super::NativeOnDeactivated();
    }
  6. The code to show party members on the UI is complete. Now, you must call several functions to perform party-related actions. Open the PartyOnlineSession_Starter class CPP file and add the code below to the respective predefined functions. What it does is call the respective party actions when the party action button is clicked. These action buttons are the generated buttons mentioned in the Add a party menu tutorial of this module.

    void UPartyOnlineSession_Starter::OnInviteToPartyButtonClicked(const int32 LocalUserNum, const FUniqueNetIdPtr& Invitee)
    {
    SendPartyInvite(LocalUserNum, Invitee);
    }

    void UPartyOnlineSession_Starter::OnKickPlayerFromPartyButtonClicked(const int32 LocalUserNum, const FUniqueNetIdPtr& KickedPlayer)
    {
    KickPlayerFromParty(LocalUserNum, KickedPlayer);
    }

    void UPartyOnlineSession_Starter::OnPromotePartyLeaderButtonClicked(const int32 LocalUserNum, const FUniqueNetIdPtr& NewLeader)
    {
    PromotePartyLeader(LocalUserNum, NewLeader);
    }

Resources