Integrate Party into your game
Overview
The AccelByte Gaming Service (AGS) Session service includes a Party 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.
- 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 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.
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.
Usage | Permissions | Action |
---|---|---|
To add, edit, and delete a Session Template | ADMIN:NAMESPACE:*:SESSION:CONFIGURATION | CREATE, READ, UPDATE, DELETE |
To view a session in Session and Parties | NAMESPACE:*:SESSION:GAME | CREATE, READ, UPDATE, DELETE |
Implement party interaction
Follow the steps below to implement party interaction into your game. Select the tab in each section for the game engine you are using (Unreal with AccelByte OnlineSubsystem (OSS) or Unity SDK) and follow the steps for that engine.
Prerequisites
- OSS
- Unity
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 following method to get FOnlineSessionV2AccelByte
and LocalPlayerId
from the game:
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();
using AccelByte.Api;
using AccelByte.Core;
Use the following method to get the API client to interact with AGS:
The rest of the tutorial will not include this code snippet to focus on each functionality. Party is part of Session API in V2 and the lobby is used to send and receive notifications about the party.
var apiClient = AccelByteSDK.GetClientRegistry().GetApi();
var lobby = apiClient.GetLobby();
var session = apiClient.GetSession();
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.
- OSS
- Unity
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);
var request = new SessionV2PartySessionCreateRequest
{
attributes = new Dictionary<string, object>(), // Add custom attributes if needed
configurationName = "config-name-from-admin-portal",
joinability = SessionV2Joinability.INVITE_ONLY,
members = Array.Empty<SessionV2MemberData>(), // Optional if you want to invite other user when party created
textChat = true // Enable to automatically create chat party topic
};
session.CreateParty(request, result =>
{
if (result.IsError)
{
// Do something if CreateParty has an error
Debug.Log($"Error CreateParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if CreateParty has been successful
});
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.
- OSS
- Unity
// Listen to party invitation
SessionInterface->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateLambda(
[](const FUniqueNetId& UserId, const FUniqueNetId&, const FOnlineSessionInviteAccelByte& InviteResult)
{
if (InviteResult.SessionType == EAccelByteV2SessionType::PartySession)
{
// Handler on party session invite notification
// InviteResult contains the party details
}
}));
// Send invitation to other user
SessionInterface->SendSessionInviteToFriend(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, OtherUserPlayerId.ToSharedRef().Get());
// Listen to party invitation notification
lobby.SessionV2InvitedUserToParty += result =>
{
if (result.IsError)
{
// Do something if SessionV2InvitedUserToParty has an error
Debug.Log($"Error SessionV2InvitedUserToParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2InvitedUserToParty is successfully received
};
// Send invitation to other user
string partyId = "current-party-id";
string otherUserId = "other-user-id";
session.InviteUserToParty(
partyId,
otherUserId,
result =>
{
if (result.IsError)
{
// Do something if InviteUserToParty has an error
Debug.Log($"Error InviteUserToParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if InviteUserToParty has been successful
}
);
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.
- OSS
- Unity
// Listen to party invitation
SessionInterface->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateLambda(
[](const FUniqueNetId& UserId, const FUniqueNetId&, const FOnlineSessionInviteAccelByte& InviteResult)
{
if (InviteResult.SessionType == EAccelByteV2SessionType::PartySession)
{
// Handler on party session invite notification
// InviteResult contains the party details
}
}));
// Add delegate on join party session completed
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 notification
SessionInterface->JoinSession(*LocalPlayerId.Get(), NAME_PartySession, Invitation.Session);
// Listen to party joined notification
lobby.SessionV2UserJoinedParty += result =>
{
if (result.IsError)
{
// Do something if SessionV2UserJoinedParty has an error
Debug.Log($"Error SessionV2UserJoinedParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2UserJoinedParty is successfully received
};
// Join the party session
string partyId = "invited-party-id";
session.JoinParty(partyId, result =>
{
if (!result.IsError)
{
if (result.IsError)
{
// Do something if JoinParty has an error
Debug.Log($"Error JoinParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if JoinParty has been successful
}
});
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.
- OSS
- Unity
FOnRejectSessionInviteComplete OnRejectSessionInviteComplete = FOnRejectSessionInviteComplete::CreateLambda([](const bool bWasSuccessful)
{
if (bWasSuccessful)
{
// Successfully reject party invitation
}
});
// Join the party session
FOnlineSessionInviteAccelByte Invitation; // The invitation from notification
SessionInterface->RejectInvite(LocalPlayerId.ToSharedRef().Get(), Invitation, OnRejectSessionInviteComplete);
// Listen for party rejected notification
lobby.SessionV2UserRejectedPartyInvitation += result =>
{
if (result.IsError)
{
// Do something if SessionV2UserRejectedPartyInvitation has an error
Debug.Log($"Error SessionV2UserRejectedPartyInvitation, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2UserRejectedPartyInvitation is successfully received
};
// Reject party invitation
string partyId = "invited-party-id";
session.RejectPartyInvitation(partyId, result =>
{
if (result.IsError)
{
// Do something if RejectPartyInvitation has an error
Debug.Log($"Error RejectPartyInvitation, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if RejectPartyInvitation has been successful
});
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.
- OSS
- Unity
// Listen for party session update
SessionInterface->AddOnSessionUpdateReceivedDelegate_Handle(FOnSessionUpdateReceivedDelegate::CreateLambda([SessionInterface](FName SessionName)
{
if (SessionName == NAME_PartySession)
{
// there is update notification for 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);
// Listen for party session update notification
lobby.SessionV2PartyUpdated += result =>
{
if (result.IsError)
{
// Do something if SessionV2PartyUpdated has an error
Debug.Log($"Error SessionV2PartyUpdated, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2PartyUpdated is successfully received
};
lobby.SessionV2PartyMemberChanged += result =>
{
if (result.IsError)
{
// Do something if SessionV2PartyMemberChanged has an error
Debug.Log($"Error SessionV2PartyMemberChanged, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2PartyMemberChanged is successfully received
};
// Promote another party member to party leader
string partyId = "current-party-id";
string leaderId = "current-party-leader-id";
session.PromoteUserToPartyLeader(
partyId,
leaderId,
result =>
{
if (result.IsError)
{
// Do something if PromoteUserToPartyLeader has an error
Debug.Log($"Error PromoteUserToPartyLeader, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if PromoteUserToPartyLeader has been successful
}
);
Kick a player from a party
The party leader has the privilege 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.
- OSS
- Unity
// Listen for kicked from party session notification
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());
// Listen for kicked from party session notification
lobby.SessionV2UserKickedFromParty += result =>
{
if (result.IsError)
{
// Do something if SessionV2UserKickedFromPart has an error
Debug.Log($"Error SessionV2UserKickedFromPart, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2UserKickedFromPart is successfully received
};
// Kick player from party session
string partyId = "current-party-id";
string otherUserId = "user-id-to-be-kicked";
session.KickUserFromParty(
partyId,
otherUserId,
result =>
{
if (result.IsError)
{
// Do something if KickUserFromParty has an error
Debug.Log($"Error KickUserFromParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if KickUserFromParty has been successful
}
);
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.
- OSS
- Unity
// Listen for party session update
SessionInterface->AddOnSessionUpdateReceivedDelegate_Handle(FOnSessionUpdateReceivedDelegate::CreateLambda([SessionInterface](FName SessionName)
{
if (SessionName == NAME_PartySession)
{
// There is 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);
// Listen for party session update notification
lobby.SessionV2PartyUpdated += result =>
{
if (result.IsError)
{
// Do something if SessionV2PartyUpdated has an error
Debug.Log($"Error SessionV2PartyUpdated, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if SessionV2PartyUpdated is successfully received
};
// Leave a party
string partyId = "current-party-id";
session.LeaveParty(partyId, result =>
{
if (result.IsError)
{
// Do something if LeaveParty has an error
Debug.Log($"Error LeaveParty, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if LeaveParty has been successful
});
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 from other players. This function allows party leaders to get the party code.
- OSS
- Unity
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);
var partyId = "current-party-id";
session.GetPartyDetails(partyId, result =>
{
if (result.IsError)
{
// Do something if GetPartyDetails has an error
Debug.Log($"Error GetPartyDetails, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if GetPartyDetails has been successful
var code = result.Value.Code;
});
Generate or refresh a party code
Use the following function to generate or refresh a party code.
- OSS
- Unity
const FOnGenerateNewPartyCodeComplete OnGenerateNewPartyCodeCompleteDelegate =
FOnGenerateNewPartyCodeComplete::CreateLambda([](const bool bWasSuccessful, FString NewPartyCode)
{
if (bWasSuccessful)
{
// successfully generate new party code
}
});
var partyId = "current-party-id";
session.GenerateNewPartyCode(partyId, result =>
{
if (result.IsError)
{
// Do something if GenerateNewPartyCode has an error
Debug.Log($"Error GenerateNewPartyCode, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if GenerateNewPartyCode has been successful
});
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.
- OSS
- Unity
const FOnRevokePartyCodeComplete OnRevokePartyCodeComplete =
FOnRevokePartyCodeComplete::CreateLambda([](const bool bWasSuccessful)
{
if (bWasSuccessful)
{
// Successfully revoked party code
}
});
SessionInterface->RevokePartyCode(LocalPlayerId.ToSharedRef().Get(), NAME_PartySession, OnRevokePartyCodeComplete);
var partyId = "current-party-id";
session.RevokePartyCode(partyId, result =>
{
if (result.IsError)
{
// Do something if RevokePartyCode has an error
Debug.Log($"Error RevokePartyCode, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if RevokePartyCode has been successful
});
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.
- OSS
- Unity
// Add delegate on join party session completed
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);
var partyCode = "targeted-party-code";
session.JoinPartyByCode(partyCode, result =>
{
if (result.IsError)
{
// Do something if JoinPartyByCode has an error
Debug.Log($"Error JoinPartyByCode, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if JoinPartyByCode has been successful
});