Joinable Sessions
Start Matchmaking for a Joinable Session
The following function is used to start matchmaking for a joinable session.
- Unreal Engine
- Unity
// make sure lobby is connected first and in a party.
// this flow is for when matched to a new session
FRegistry::Lobby.SetMatchmakingNotifDelegate(THandler<FAccelByteModelsMatchmakingNotice>::CreateLambda([&lobby](const FAccelByteModelsMatchmakingNotice& Result)
{
// matchmaking is completed when it's notif status is done, then send ready consent.
if(Result.Status == EAccelByteMatchmakingStatus::Done)
FRegistry::Lobby.SendReadyConsentRequest(Result.MatchId);
}));
// When matched to a new session, Lobby will give DS notification after all matched player give ready consent.
// when matched to an existing session, lobby will directly give DS Notification, no ready consent required.
FRegistry::Lobby.SetDsNotifDelegate(THandler<FAccelByteModelsDsNotice>::CreateLambda([&lobby](const FAccelByteModelsDsNotice& Result)
{
if (Result.Status == "READY" || Result.Status == "BUSY")
{
// DS is ready to be joined
}
}));
FRegistry::Lobby.SendStartMatchmaking("game_mode");
Lobby lobby = AccelBytePlugin.GetLobby();
// ensure lobby connected and in a party to start matchmaking
// this flow is for when matched to a new session
lobby.MatchmakingCompleted += (response) =>
{
if(response.Value.status.ToLower() == "done")
{
lobby.ConfirmReadyForMatch(response.Value.matchId, result => Debug.Log("sending ready consent"));
}
};
// When matched to a new session, Lobby will give DS notification after all matched player give ready consent.
// when matched to an existing session, lobby will directly give DS Notification, no ready consent required.
lobby.DSUpdated += (dsUpdate) =>
{
string dsStatus = dsUpdate.Value.status.ToLower();
if (dsStatus == "ready" || dsStatus == "busy")
{
// DS is ready to be joined
}
};
lobby.StartMatchmaking("game_mode", result => Debug.Log("starting matchmaking"));
Check a Joinable Session's Status
When a server is claimed, it will query the session details from the Matchmaking service. The game server will determine whether the session is joinable or not by calling the following function.
- Unreal Engine
- Unity
FString MatchId = "testMatchId";
FAccelByteMatchmakingResult SessionData;
AccelByte::FRegistry::ServerDSM.QuerySessionStatus(
MatchId,
THandler<FAccelByteMatchmakingResult>::CreateLambda(
[&SessionData](const FAccelByteMatchmakingResult& Result)
{
SessionData = Result;
if (Result.joinable)
{
UE_LOG(LogTemp, Log, TEXT("Session is joinable"));
}
else
{
UE_LOG(LogTemp, Log, TEXT("Session is joinable"));
}
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Log, TEXT("Error query session status"));
}
));
var mm = AccelByteServerPlugin.GetMatchmaking();
string matchId = "placeholderMatchId";
mm.QuerySessionStatus(matchId, result =>
{
Debug.Log("Session is " + (result.Value.joinable ? "JOINABLE" : "NonJoinable"));
});
Enqueue a Session
When receiving a joinable session, the game server will enqueue the session to the Matchmaking service as soon as it is ready to accept more players. To enqueue a session, use the following function.
- Unreal Engine
- Unity
AccelByte::FRegistry::ServerDSM.EnqueueJoinableSession(
SessionData,
FVoidHandler::CreateLambda([]()
{
UE_LOG(LogTemp, Log, TEXT("Enqueue Session Success"));
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Log, TEXT("Enqueue Session Success Failed"));
}
));
var mm = AccelByteServerPlugin.GetMatchmaking();
MatchmakingResult sessionData = null; //get session data using QuerySessionStatus
mm.EnqueueJoinableSession(sessionData, result =>
{
if (result.IsError)
{
Debug.Log("Enqueue joinable session error " + result.Error.Code + " with message " + result.Error.Message);
}
else
{
Debug.Log("Enqueue joinable session success");
}
});
After the game server enqueues a session, players who are already matched into the session can connect to the Lobby.
Update a Session
To ensure that the session in the queue is accurate, any time a player leaves during the Lobby phase, the server will update the session in the queue. To make this update, use the function below.
- Unreal Engine
- Unity
FString gameMode = sessionQueryResult.Game_mode;
FString matchId = sessionQueryResult.Match_id;
FString userId;
FRegistry::ServerMatchmaking.RemoveUserFromSession(gameMode, matchId, userId, FVoidHandler::CreateLambda([]()
{
UE_LOG(LogTemp, Log, TEXT("Remove user success"));
}), FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Log, TEXT("Remove user Failed"));
}));
// get session query value from QuerySessionStatus() method
var sessionQueryResult = sessionQuery.Value;
string leavingUserId = "UserId";
mm.RemoveUserFromSession(sessionQueryResult.channel, sessionQueryResult.match_id, leavingUserId, result =>
{
if (result.IsError)
{
Debug.Log("Remove User failed with code " + result.Error.Code + " message " + result.Error.Message);
}
else
{
Debug.Log("Remove User Success");
}
});
Dequeue a Session
When the game is about to start, the game server will dequeue the session to prevent the Matchmaking service from matching a new player into it. To dequeue a session, use the following function.
- Unreal Engine
- Unity
FString matchId;
AccelByte::FRegistry::ServerDSM.DequeueJoinableSession(
matchId,
FVoidHandler::CreateLambda([]()
{
UE_LOG(LogTemp, Log, TEXT("Enqueue Session Success"));
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Log, TEXT("Enqueue Session Success Failed"));
}
));
var mm = AccelByteServerPlugin.GetMatchmaking();
string matchId = "placeholderMatchId";
mm.DequeueJoinableSession(matchId, result =>
{
if (result.IsError)
{
Debug.Log("session dequeue error " + result.Error.Code + " with message " + result.Error.Message);
}
else
{
Debug.Log("session dequeue success");
}
});