Configuring Session Templates with the SDK
Last updated on November 20, 2023
Overview
This section shows how to create, update, and otherwise manage a session through the Game SDK.
Session Templates enable a game developer to configure how sessions behave.
The goals of this topic are to provide code snippets showing how to:
- Create a game session
- Retrieve a game session
- Retrieve game session details
- Update a game session
- Delete a game session
- Invite a player to a game session -
- Join a game session
- Reject a game session invite
- Leave a game session
- Listen to game session notifications
Managing a Game session using the Game SDK
Create a Game Session
- Unreal Engine
- Unity
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 success
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when operation error / failed
}));
SessionV2GameSessionCreateRequest createGameSessionRequest = new SessionV2GameSessionCreateRequest
{
joinability = SessionV2Joinability.OPEN,
configurationName = _configurationTemplateName
};
AccelBytePlugin.GetSession()
.CreateGameSession(createGameSessionRequest, result =>
{
if (!result.IsError)
{
// Creating game session success
}
});
Retrieve a Game Session
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.GetMyGameSessions(
THandler<FAccelByteModelsV2PaginatedGameSessionQueryResult>::CreateLambda(
[&](const FAccelByteModelsV2PaginatedGameSessionQueryResult& Result)
{
// Do something when operation success
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when operation error / failed
}));
AccelBytePlugin.GetSession()
.GetUserGameSessions(null, null, null, result =>
{
if (!result.IsError)
{
// success get game session details
}
});
Retrieve Game Session details
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.GetGameSessionDetails( SessionId,
THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// Do something when operation success
}),
FErrorHandler::CreateLambda(
[&](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something when operation error / failed
}));
AccelBytePlugin.GetSession()
.GetGameSessionDetailsBySessionId(sessionId, result =>
{
if (!result.IsError)
{
// success get Game Session details
}
});
Update a Game Session
- Unreal Engine
- Unity
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 success
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// do something when failed / error
}));
SessionV2GameSessionUpdateRequest updateRequest = new SessionV2GameSessionUpdateRequest()
{
joinability = SessionV2Joinability.OPEN
};
AccelBytePlugin.GetSession()
.PatchGameSession(sessionId, updateRequest, result =>
{
if (!result.IsError)
{
// Update game session success
}
});
Delete a Game Session
- Unreal Engine
- Unity
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
}));
AccelBytePlugin.GetSession()
.DeleteGameSession(sessionId, result =>
{
if (!result.IsError)
{
// delete Game Session success
}
});
Invite a Player to a Game Session
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.SendGameSessionInvite(SessionId, UserIdToInvite, FVoidHandler::CreateLambda(
[&]
{
// do something when success
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// do something when failed / error
}));
AccelBytePlugin.GetSession()
.InviteUserToGameSession(sessionId, userIdToInvite, result =>
{
if (!result.IsError)
{
// invite to game session success
}
});
Join a Game Session
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.JoinGameSession(SessionId, THandler<FAccelByteModelsV2GameSession>::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Result)
{
// do something when success
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// do something when failed / error
}));
AccelBytePlugin.GetSession()
.JoinGameSession(sessionId, result =>
{
if (!result.IsError)
{
// join to game session success
}
});
Reject a Game Session Invite
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.RejectGameSessionInvite(GameSessionID, FVoidHandler::CreateLambda(
[&]
{
// do something when success
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// do something when failed / error
}));
AccelBytePlugin.GetSession()
.RejectGameSessionInvitation(sessionId, result =>
{
if (!result.IsError)
{
// reject to game session success
}
});
Leave a Game Session
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Session.LeaveGameSession(SessionId, FVoidHandler::CreateLambda(
[&]
{
// do something when success
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& Message)
{
// do something when failed / error
}));
AccelBytePlugin.GetSession()
.LeaveGameSession(sessionId, result =>
{
if (!result.IsError)
{
// leave to game session success
}
});
Listen to Game Session Notifications
- Unreal Engine
- Unity
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
ApiClient->Lobby.SetV2GameSessionInvitedNotifDelegate(Api::Lobby::FV2GameSessionInvitedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserInvitedEvent& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2GameSessionMembersChangedNotifDelegate(Api::Lobby::FV2GameSessionMembersChangedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionMembersChangedEvent& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2GameSessionJoinedNotifDelegate(Api::Lobby::FV2GameSessionJoinedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserJoinedEvent& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2GameSessionRejectedNotifDelegate(Api::Lobby::FV2GameSessionRejectedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserRejectedEvent& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2GameSessionKickedNotifDelegate(Api::Lobby::FV2GameSessionKickedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSessionUserKickedEvent& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2GameSessionUpdatedNotifDelegate(Api::Lobby::FV2GameSessionUpdatedNotif::CreateLambda(
[&](const FAccelByteModelsV2GameSession& Notif)
{
// Do something when receive notification
}));
ApiClient->Lobby.SetV2DSStatusChangedNotifDelegate(Api::Lobby::FV2DSStatusChangedNotif::CreateLambda(
[&](const FAccelByteModelsV2DSStatusChangedNotif& Notif)
{
// Do something when receive notification
}));
AccelBytePlugin.GetLobby().SessionV2GameSessionUpdated += (Result<SessionV2GameSessionUpdatedNotification> notif) =>
{
// do something when a game session is updated
};
AccelBytePlugin.GetLobby().SessionV2GameSessionMemberChanged += (Result<SessionV2GameMembersChangedNotification> notif) =>
{
// do something when a game session member has change
};
AccelBytePlugin.GetLobby().SessionV2UserJoinedGameSession += (Result<SessionV2GameJoinedNotification> notif) =>
{
// do something when a user joined the game session
};
AccelBytePlugin.GetLobby().SessionV2UserKickedFromGameSession += (Result<SessionV2GameUserKickedNotification> notif) =>
{
// do something when kicked from a game session
};
AccelBytePlugin.GetLobby().SessionV2UserRejectedGameSessionInvitation += (Result<SessionV2GameInvitationRejectedNotification> notif) =>
{
// do something when a game session invite is rejected
};
AccelBytePlugin.GetLobby().SessionV2InvitedUserToGameSession += (Result<SessionV2GameInvitationNotification> notif) =>
{
// do something when invited to a game session
};