Skip to main content

Browse match sessions with the SDK - Browse dedicated server joinable sessions - (Unity module)

Last updated on November 25, 2024

Unwrap the wrapper

In this tutorial, you will learn how to browse match sessions using the AccelByte Gaming Services (AGS) Game SDK. In the Byte Wars, project there is already an SDK wrapper created named BrowseMatchSessionWrapper_Starter.cs. This class will call the AGS Game SDK API to browse match sessions. In this tutorial, you will use a starter version of the wrapper called BrowseMatchSessionWrapper_Starter to follow along from scratch.

As mentioned above, MatchSessionDSWrapper_Starter.cs is a subclass of the MatchSessionWrapper.cs class that already implements several functions for browsing and joining a match session.

A function called JoinMatchSession has already been created. In this function, the leave session event is bound and calls the JoinSession function to join a game session.

protected internal void JoinMatchSession(string sessionId, InGameMode gameMode)
{
GameManager.Instance.OnClientLeaveSession += LeaveCurrentGameSession;
GameManager.OnDisconnectedInMainMenu += DisconnectFromServer;
lobby.SessionV2GameSessionMemberChanged += OnV2GameSessionMemberChanged;
SelectedGameMode = gameMode;

JoinSession(sessionId);
}

The BrowseMatchSession function uses the QueryGameSession method (defined in the Introduction to Session module) to search for game sessions based on specified parameters.

protected internal void BrowseMatchSession(Dictionary<string, object> request)
{
QueryGameSession(request);
}

What's in the Starter Pack

The BrowseMatchSessionWrapper_Starter is a subclass of MatchSessionWrapper, which has all the functions from its parent. The BrowseMatchSessionWrapper_Starter class has several features related to session management.

Implement browse match session

In this section, you will implement the AGS Game SDK API call to browse match sessions.

  1. Open BrowseMatchSessionWrapper_Starter.cs. Modify the BrowseMatch function as shown below. The AGS Game SDK API call for browsing game sessions is BrowseMatchSession which inherited from MatchSessionWrapper.cs

    protected internal void BrowseMatch(Action<BrowseMatchResult> onSessionRetrieved)
    {
    nextPage = string.Empty;
    isBrowseMatchSessionsCanceled = false;
    onQueryMatchSessionFinished = onSessionRetrieved;
    BrowseMatchSession(CreateMatchConfig.CreatedMatchSessionAttribute);
    }
  2. Modify the OnBrowseMatchSessionsComplete function as shown below. The function will handle the results for browsing game sessions.

    private void OnBrowseMatchSessionsComplete(Result<PaginatedResponse<SessionV2GameSession>> result)
    {
    if (!result.IsError)
    {
    if (!isBrowseMatchSessionsCanceled)
    {
    onQueryMatchSessionFinished?.Invoke(new BrowseMatchResult(result.Value.data));
    }
    nextPage = result.Value.paging.next;
    }
    else
    {
    if (!isBrowseMatchSessionsCanceled)
    {
    onQueryMatchSessionFinished?.Invoke(new BrowseMatchResult(null, result.Error.Message));
    }
    }
    }

Resources