Skip to main content

Integrate Party into your game

Last updated on October 24, 2024

Overview

AccelByte Gaming Services (AGS) Session includes AGS Party, a service which handles parties for multiplayer play. Players can interact with parties in several ways, including:

  • User Party Info: shows players information about their party, including the Party ID, Leader ID, and the list of party members.
  • Create Party: allows players to create a new party that other players can join.
  • Invite to Party: allows players to invite their friends to join their party. Both the inviter and invitee will receive a notification when the invitation is sent.
  • Join Party: allows players to join a party that they've been invited to join. When the player joins the party, the inviter and other party members will be notified.
  • Cancel Party Invitation allows players to cancel a party invitation. Both invitees and inviters will receive a notification that the invitation has been canceled. Invitees can't join the previous invitation after it is canceled.
  • Reject Party Invitation: allows players to reject a party invitation. The inviter will be notified that their invitation was rejected.
  • Promote a Member as a Party Leader: allows players to set another player as the party leader.
  • Kick from Party: allows party leaders to remove (kick) another player from their party.
  • Leave Party: allows players to leave a party that they have already joined. The other party members will be notified when a player leaves their party.

This article will cover integrating player parties using AGS Party.

Permissions

Permissions are used to grant access to specific resources within AGS. Make sure your account has the following permissions before you attempt to manage Party Session V2.

UsagePermissionsAction
To add, edit, and delete a Session TemplateADMIN:NAMESPACE:*:SESSION:CONFIGURATIONCREATE, READ, UPDATE, DELETE
To view a session in Session and PartiesNAMESPACE:*:SESSION:GAMECREATE, READ, UPDATE, DELETE

Prerequisites

Include the following files to use Session Interface V2:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineSessionInterface.h"
#include "Engine/World.h"
#include "Engine/GameEngine.h"

#include "OnlineSessionInterfaceV2AccelByte.h"
#include "OnlineSubsystemAccelByteSessionSettings.h"
#include "OnlineSubsystemSessionSettings.h"

Use the function below to get FOnlineSessionV2AccelByte and LocalPlayerId from the game.

note

The rest of the tutorial will not include this code snippet to focus on each functionality of the interface.

// Get FOnlineSessionV2AccelByte from the game
const UWorld* World = GEngine->GetCurrentPlayWorld();
const IOnlineSubsystem* Subsystem = Online::GetSubsystem(World, ACCELBYTE_SUBSYSTEM);
const auto SessionInterface = StaticCastSharedPtr<FOnlineSessionV2AccelByte>(Subsystem->GetSessionInterface());

// Get LocalPlayerId from the game
const int PlayerIndex = 0;
const ULocalPlayer* LocalPlayer = GEngine->GetLocalPlayerFromControllerId(GEngine->GetCurrentPlayWorld(), PlayerIndex );
const FUniqueNetIdPtr LocalPlayerId = LocalPlayer->GetPreferredUniqueNetId().GetUniqueNetId();

Create a party

Players can create a party that can be used for matchmaking. A player can only create one party at a time, and they will become the leader of the party they create.

SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(
FOnCreateSessionCompleteDelegate::CreateLambda([](const FName SessionName, const bool bWasSuccessful)
{
if (SessionName == NAME_PartySession)
{
if(!bWasSuccessful)
{
// Create party error handling
}
// Party created handling
}
}));

FOnlineSessionSettings NewSessionSettings;
NewSessionSettings.NumPrivateConnections = 4;

NewSessionSettings.Set(SETTING_SESSION_JOIN_TYPE, TEXT("INVITE_ONLY"));
NewSessionSettings.Set(SETTING_SESSION_TYPE, SETTING_SESSION_TYPE_PARTY_SESSION);
NewSessionSettings.Set(SETTING_SESSION_TEMPLATE_NAME, TEXT("TemplateNameFromAP"));

SessionInterface->CreateSession(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, NewSessionSettings);

Invite to party

Players can invite other players to their party. Invitees will receive a notification that they have been invited to join a party and current party members will also receive a notification that someone has been invited to the party.

// Listen for party invitations
SessionInterface->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateLambda(
[](const FUniqueNetId& UserId, const FUniqueNetId&, const FOnlineSessionInviteAccelByte& InviteResult)
{
if (InviteResult.SessionType == EAccelByteV2SessionType::PartySession)
{
// Handle party session invite notifications
// InviteResult contains the party details
}
}));

// Send invitation to other user
SessionInterface->SendSessionInviteToFriend(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, OtherUserPlayerId.ToSharedRef().Get());

Cancel outgoing party invitation

Players can cancel outgoing invitations. Invitees and inviters will receive a notification that the invitation has been canceled. Invitees can't accept the previous invitation after it is canceled.

// Invitee and inviter listen for session invite canceled notification.
auto OnSessionInviteCanceledDelegate = SessionInterface->AddOnSessionInviteCanceledDelegate_Handle(
FOnSessionInviteCanceledDelegate::CreateLambda([](const FString& SessionID) // ID of canceled party.
{
// Received invitation canceled.
}));

// Party leader (inviter) cancel invitation.
FName PartyName = NAME_PartySession;
auto OnCancelSessionInviteCompleteDelegate = SessionInterface->AddOnCancelSessionInviteCompleteDelegate_Handle(FOnCancelSessionInviteCompleteDelegate::CreateLambda(
[&](const FUniqueNetId& LocalUserId, FName SessionName, const FUniqueNetId& Invitee, const FOnlineError& ErrorInfo)
{
bool bInvitationCanceled = ErrorInfo.bSucceeded;
// Cancel session invitation complete.
}));

SessionInterface->CancelSessionInvite(PlayerIndex, PartyName, *InviteeUniqueNetId);

Join a party

Players that are invited to a party can accept the invitation and join the party. Party invitees will receive a notification that they've joined the party and current party members will also receive a notification that someone is joining the party.

// Listen for party invitations
SessionInterface->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateLambda(
[](const FUniqueNetId& UserId, const FUniqueNetId&, const FOnlineSessionInviteAccelByte& InviteResult)
{
if (InviteResult.SessionType == EAccelByteV2SessionType::PartySession)
{
// Handle party session invite notifications
// InviteResult contains the party details
}
}));

// Add delegate for joining a party session completing
SessionInterface->AddOnJoinSessionCompleteDelegate_Handle(
FOnJoinSessionCompleteDelegate::CreateLambda([](FName SessionName, EOnJoinSessionCompleteResult::Type Result){
if (SessionName == NAME_PartySession)
{
// Join party session completed, check the result
}
}));

// Join the party session
FOnlineSessionInviteAccelByte Invitation; // The invitation from the notification
SessionInterface->JoinSession(*LocalPlayerId.Get(), NAME_PartySession, Invitation.Session);

Reject a party invitation

When a player has been invited to a party, they can choose to reject the invitation. Party invitees and current party members will receive a notification that they've rejected the invitation.

FOnRejectSessionInviteComplete OnRejectSessionInviteComplete = FOnRejectSessionInviteComplete::CreateLambda([](const bool bWasSuccessful)
{
if (bWasSuccessful)
{
// Successfully rejected party invitation
}
});

// Join the party session
FOnlineSessionInviteAccelByte Invitation; // The invitation from the notification
SessionInterface->RejectInvite(LocalPlayerId.ToSharedRef().Get(), Invitation, OnRejectSessionInviteComplete);

Promote a member to party leader

The party leader can promote another party member to become the new party leader. All party members will be notified of the new party leader. This is how the party leader can promote another party member to become a new party leader. The new party leader is identified by a specific user ID from a party member.

// Listen for party session updates
SessionInterface->AddOnSessionUpdateReceivedDelegate_Handle(FOnSessionUpdateReceivedDelegate::CreateLambda([SessionInterface](FName SessionName)
{
if (SessionName == NAME_PartySession)
{
// There is an update notification for the party
auto LatestPartySession = SessionInterface->GetNamedSession(NAME_PartySession);
}
}));

// Promote another party member to party leader
FOnPromotePartySessionLeaderComplete OnPromotePartySessionLeaderComplete =
FOnPromotePartySessionLeaderComplete::CreateLambda([](const FUniqueNetId& PromotedUserId, const FOnlineError& Result)
{
if (Result.bSucceeded)
{
// Successfully promoted new party leader
}
});

SessionInterface->PromotePartySessionLeader(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, OtherUserPlayerId.ToSharedRef().Get(), OnPromotePartySessionLeaderComplete);

Kick a player from a party

The party leader has the ability to remove a party member from their party by kicking them out of the party. The kicked party member will receive a notification that they've been kicked out of the party, and current party members will also receive a notification that someone has been kicked from the party.

// Listen for notifications for kicking players from a party session
SessionInterface->AddOnKickedFromSessionDelegate_Handle(FOnKickedFromSessionDelegate::CreateLambda([](FName SessionName)
{
if (SessionName == NAME_PartySession)
{
// Player kicked from party
}
}));

// Kick player from party session
FOnKickPlayerComplete OnKickPlayerComplete =
FOnKickPlayerComplete::CreateLambda([](const bool bWasSuccessful, const FUniqueNetId& KickedPlayerId)
{
if (bWasSuccessful)
{
// Player successfully kicked from the party
}
});

SessionInterface->KickPlayer(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, PlayerIdToKick.ToSharedRef().Get());

Leave a party

Party members can choose to leave their party. If the party leader leaves the party, party leadership will be passed to another party member automatically. Party members that leave the party will receive a notification that they've left the party and current party members will also receive a notification that someone is leaving the party.

// Listen for party session update
SessionInterface->AddOnSessionUpdateReceivedDelegate_Handle(FOnSessionUpdateReceivedDelegate::CreateLambda([SessionInterface](FName SessionName)
{
if (SessionName == NAME_PartySession)
{
// There is an update notification for party
auto LatestPartySession = SessionInterface->GetNamedSession(NAME_PartySession);
}
}));

// Leave a party
PartySession = SessionInterface->GetNamedSession(NAME_PartySession);
auto PartySessionInfo = StaticCastSharedPtr<FOnlineSessionInfoAccelByteV2>(PartySession->SessionInfo);
FString PartyId = PartySessionInfo->GetBackendSessionDataAsPartySession()->ID;

FOnLeaveSessionComplete OnLeavePartyComplete =
FOnLeaveSessionComplete::CreateLambda([](const bool bWasSuccessful, FString SessionId)
{
if (bWasSuccessful)
{
// Successfully left a party
}
});
SessionInterface->LeaveSession(LocalPlayerId.ToSharedRef().Get(), EAccelByteV2SessionType::PartySession, PartyId, OnLeavePartyComplete);

Party codes

Party leaders can send party codes to players so that those players can join their party. Only party leaders can refresh or revoke a party code.

Get a party code

Once a code is generated, party leaders can get the code and share it to other players. This function allows party leaders to get the party code.

FString Code;
auto PartySession = SessionInterface->GetNamedSession(NAME_PartySession);
auto PartySessionInfo = StaticCastSharedPtr<FOnlineSessionInfoAccelByteV2>(PartySession->SessionInfo);
FString PartyCode = PartySessionInfo->GetBackendSessionDataAsPartySession()->Code;

PartySession->SessionSettings.Get(SETTING_PARTYSESSION_CODE, PartyCode);

Generate or refresh a party code

Use the following function to generate or refresh a party code.

const FOnGenerateNewPartyCodeComplete OnGenerateNewPartyCodeCompleteDelegate =
FOnGenerateNewPartyCodeComplete::CreateLambda([](const bool bWasSuccessful, FString NewPartyCode)
{
if (bWasSuccessful)
{
// successfully generate new party code
}
});

Revoke a party code

Party leaders can revoke a party code. If a party code is revoked, players will no longer be able to use it to join the party. This function allows party leaders to revoke the party code.

const FOnRevokePartyCodeComplete OnRevokePartyCodeComplete =
FOnRevokePartyCodeComplete::CreateLambda([](const bool bWasSuccessful)
{
if (bWasSuccessful)
{
// Successfully revoked party code
}
});

SessionInterface->RevokePartyCode(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, OnRevokePartyCodeComplete);

Join a party with a party code

Players that have been sent a party code can use it to join a party. This function allows players to join a party using a party code.

// Add delegate for the completion of joining a party session
SessionInterface->AddOnJoinSessionCompleteDelegate_Handle(
FOnJoinSessionCompleteDelegate::CreateLambda([](FName SessionName, EOnJoinSessionCompleteResult::Type Result){
if (SessionName == NAME_PartySession)
{
// Join party session completed, check the result
}
}));

// Party code from party leader
FString PartyCode = TEXT("PARTY_CODE");
SessionInterface->JoinSession(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, PartyCode);

Refresh party session

The game session can be refreshed to synchronize with the backend using the Refresh Session method. The provided SessionName parameter may correspond to NAME_PartySession.

SessionInterface->RefreshSession(SessionName, FOnRefreshSessionComplete::CreateLambda([]()
{
// On refresh session complete
}));

Refresh active sessions

To prevent issues and ensure proper synchronization with the backend, use the following function to refresh all active sessions that are cached locally on the client:

SessionInterface->RefreshActiveSessions(FOnRefreshActiveSessionsComplete::CreateLambda(
[&](bool bWasSuccessful)
{
// On refresh active sessions complete
}));