Skip to main content

Integrate game session

Last updated on August 15, 2024

Overview

You can manage game sessions with AccelByte Gaming Services (AGS) Session through the AGS Game SDK or Extend SDK. This article walks you through integrating game sessions and provides code snippets for:

  • Creating a game session
  • Querying a game session list
  • Retrieving the current user's game sessions
  • Retrieving game session details
  • Updating a game session
  • Deleting a game session
  • Inviting a player to a game session
  • Joining a game session
  • Cancelling a game session invite
  • Rejecting a game session invite
  • Leaving a game session
  • Promoting a game session leader
  • Kicking a player from a game session
  • Listening to game session notifications

Create a game session

Create a new game session based on the creation request.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

FAccelByteModelsV2GameSessionCreateRequest Request;
Request.ConfigurationName = ConfigurationName; // MANDATORY
Request.Joinability = EAccelByteV2SessionJoinability::INVITE_ONLY; // Optional
Request.Type = EAccelByteV2SessionConfigurationServerType::DS; // Optional
Request.ClientVersion = GameServerVersion; // Optional
Request.ServerName = LocalServerName; // Optional
Request.Deployment = Deployment; // Optional
Request.RequestedRegions = {"us-west-1", "us-west2"}; // Optional

// Optional
TArray<FString> TeamA = {TeamAUserId1, TeamAUserId2};
TArray<FString> TeamB = {TeamBUserId1, TeamBUserId2};
TArray<FAccelByteModelsV2GameSessionTeam> Teams;
Teams.Add({TeamA});
Request.Teams = Teams;

// Optional
Request.Attributes.JsonObject = MakeShared<FJsonObject>();
Request.Attributes.JsonObject->SetStringField("PartyAttribute", "Attribute1");

Request.MaxPlayers = 10; // Optional
Request.MinPlayers = 1; // Optional
Request.InactiveTimeout = 30; // Optional
Request.InviteTimeout = 86400; // Optional


ApiClient->Session.CreateGameSession(Request,
THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// Do something when operation is successful
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when operation fails or has an error
}));

Query a game session list

Query all the game sessions that have the same attribute as the request.

note

The code snippet for Unreal will be added soon.

Retrieve the current user's game sessions

Retrieve the list of the user's game sessions and its information.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.GetMyGameSessions(
THandler<FAccelByteModelsV2PaginatedGameSessionQueryResult>::CreateLambda(
[&](const FAccelByteModelsV2PaginatedGameSessionQueryResult& Result)
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when the operation fails or has an error
}));

Retrieve game session details

Retrieve the specific game session details by specifying the game session identity.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.GetGameSessionDetails( SessionId,
THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when the operation fails or has an error
}));

Update a game session

Update the game session's data from the specific game session.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

FAccelByteModelsV2GameSessionUpdateRequest Request;
Request.Version = PartyDataVersion; // Mandatory, must be the same version as current data in the backend
Request.Joinability = EAccelByteV2SessionJoinability::INVITE_ONLY; // Optional
Request.Attributes.JsonObject = MakeShared<FJsonObject>(); // Optional
Request.Attributes.JsonObject->SetStringField("AttributeName", "Attribute1"); // Optional
Request.MaxPlayers = 10; // Optional
Request.MinPlayers = 1; // Optional
Request.InactiveTimeout = 30; // Optional
Request.InviteTimeout = 86400; // Optional

ApiClient->Session.UpdateGameSession(GameSessionID, Request, THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

Delete a game session

Remove the existing game session.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.DeleteGameSession(SessionId, FVoidHandler::CreateLambda([]
{
// Successfully deleted game session
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& Message)
{
// Error deleting game session
}));

Invite a player to a game session

Invite other players to join the game session by specifying their user identity.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.SendGameSessionInvite(SessionId, UserIdToInvite, FVoidHandler::CreateLambda(
[&]
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// Do something when the operation fails or has an error
}));

Cancel outgoing game session 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 game session.
{
// Received invitation canceled.
}));

// Game session leader (inviter) cancel invitation.
FName GameSessionName = NAME_GameSession;
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, GameSessionName, *InviteeUniqueNetId);

Join a game session

Join the existing game session by specifying the session identity.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.JoinGameSession(SessionId, THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

Reject a game session invitation

Reject another player's invitation to the join the game session.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.RejectGameSessionInvite(GameSessionID, FVoidHandler::CreateLambda(
[&]
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

Leave a game session

Leave the current joined game session by specifying its game session identity.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.LeaveGameSession(SessionId, FVoidHandler::CreateLambda(
[&]
{
// Do something when the operation succeeds
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

Promote a game session leader

A game session leader can promote another member to become a game session leader. Since there is only one game session leader, the previous leader will become a normal member.

note

The code snippet for Unreal will be added soon.

Kick a player from a game session

A game session leader or a dedicated server can kick a member from a game session.

const FString GameSessionID = TEXT("game-session-id");
const FString PlayerID = TEXT("player-id");

// Game session leader kick a player from a game session
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Session.KickUserFromGameSession(GameSessionID, PlayerID
, FVoidHandler::CreateLambda([]()
{
// Do something when the operation succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

// Dedicated server kicks a player from a game session
FServerApiClientPtr ServerApiClient = FMultiRegistry::GetServerApiClient();
ServerApiClient->ServerSession.KickUserFromGameSession(GameSessionID, PlayerID
, FVoidHandler::CreateLambda([]()
{
// Do something when the operation succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& Message)
{
// Do something when operation fails or has an error
}));

Listen to game session notifications

There are some game session notifications that can be listened to in order to receive various updates from the game session. This uses a WebSocket connection to ensure that the notification can be received in real time.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

ApiClient->Lobby.SetV2GameSessionInvitedNotifDelegate(Api::Lobby::FV2GameSessionInvitedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserInvitedEvent& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2GameSessionMembersChangedNotifDelegate(Api::Lobby::FV2GameSessionMembersChangedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionMembersChangedEvent& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2GameSessionJoinedNotifDelegate(Api::Lobby::FV2GameSessionJoinedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserJoinedEvent& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2GameSessionRejectedNotifDelegate(Api::Lobby::FV2GameSessionRejectedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserRejectedEvent& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2GameSessionKickedNotifDelegate(Api::Lobby::FV2GameSessionKickedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserKickedEvent& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2GameSessionUpdatedNotifDelegate(Api::Lobby::FV2GameSessionUpdatedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Notif)
{
// Do something when a notification is received
}));

ApiClient->Lobby.SetV2DSStatusChangedNotifDelegate(Api::Lobby::FV2DSStatusChangedNotif::CreateLambda(
[&](const FAccelByteModelsV2DSStatusChangedNotif& Notif)
{
// Do something when a notification is received
}));