Created
March 20, 2023 21:31
-
-
Save kamyker/8086767d05e11e4e39f73507fd38c432 to your computer and use it in GitHub Desktop.
Async vs Callback example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async UniTask QuickPlayMatchmakingEOSAsync() | |
{ | |
try | |
{ | |
var lobby = AccelBytePlugin.GetLobby(); | |
if(!lobby.IsConnected) | |
lobby.Connect(); | |
string matchPoolName = "quickplay_1v1"; | |
MatchmakingV2CreateTicketRequestOptionalParams optionals = new(); | |
//Result<T>.Error is gone, error is thrown as AccelByteException | |
var ticket = await AccelBytePlugin.GetMatchmaking().CreateMatchmakingTicket(matchPoolName, optionals); | |
bool matchFound = false; | |
string sessionId = null; | |
while(matchFound == false) | |
{ | |
await UniTask.Delay(3000); | |
var ticketStatus = await AccelBytePlugin.GetMatchmaking().GetMatchmakingTicket(ticket.matchTicketId); | |
matchFound = ticketStatus.matchFound; | |
if(matchFound) | |
sessionId = ticketStatus.sessionId; | |
Debug.Log("matchFound: " + matchFound); | |
} | |
var details = await AccelBytePlugin.GetSession().GetGameSessionDetailsBySessionId(sessionId); | |
ErrorCode errorCode = ErrorCode.None; | |
var atts = details.attributes ?? new(); | |
// (...) etc | |
// END OF SAMPLE | |
} | |
catch(Exception e) | |
{ | |
Debug.LogException(e); | |
ShowErrorPopup(e.ToString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool HandleMatchmakingError<T>(Result<T> result) | |
{ | |
if(result.IsError) | |
{ | |
Debug.LogError(result.Error.Message); | |
ShowErrorPopup(result.Error); //then go back to menu, retry matchmaking | |
} | |
return result.IsError; | |
} | |
void QuickPlayMatchmakingEOS() | |
{ | |
var lobby = AccelBytePlugin.GetLobby(); | |
if(!lobby.IsConnected) | |
lobby.Connect(); | |
string matchPoolName = "quickplay_1v1"; | |
MatchmakingV2CreateTicketRequestOptionalParams optionals = new(); | |
AccelBytePlugin.GetMatchmaking().CreateMatchmakingTicket(matchPoolName, optionals, OnCreateMatchmakingTicket); | |
} | |
void OnCreateMatchmakingTicket(Result<MatchmakingV2CreateTicketResponse> result) | |
{ | |
if(HandleMatchmakingError(result)) | |
return; | |
StartCoroutine(CheckTicketStatus(result.Value.matchTicketId)); | |
} | |
IEnumerator CheckTicketStatus(string ticket) | |
{ | |
bool matchFound = false; | |
string sessionId = null; | |
while(matchFound == false) | |
{ | |
yield return new WaitForSeconds(3); | |
AccelBytePlugin.GetMatchmaking().GetMatchmakingTicket(ticketResponse.Value.matchTicketId, result => | |
{ | |
if(HandleMatchmakingError(result)) | |
return; | |
matchFound = result.Value.matchFound; | |
if(matchFound) | |
sessionId = result.Value.sessionId; | |
}); | |
Debug.Log("matchFound: " + matchFound); | |
} | |
AccelBytePlugin.GetSession().GetGameSessionDetailsBySessionId(sessionId, OnGameSessionDetails); | |
} | |
void OnGameSessionDetails(Result<SessionV2GameSession> result) | |
{ | |
if(HandleMatchmakingError(result)) | |
return; | |
SessionV2GameSession details = result.Value; | |
ErrorCode errorCode = ErrorCode.None; | |
var atts = details.attributes ?? new(); | |
// (...) etc | |
// END OF SAMPLE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment