Registering Player

Players must register to the lobby server before accessing other lobby APIs.

Please visit the Lobby API documentation.

pageRegister Player

In this tutorial, we allow players to pick their own names. The starter project already implemented the popup view to let players enter their names. You can use the ShowRegisterPlayerPopup(Action<bool, string>) method on the LobbyGUI object to display the popup view.

If the player entered a name and clicked the OK button in the popup, the ok bool value will be true and playerName string value will be the name that the player entered.

Checking into SocketWeaver

For this tutorial, We are going to use the player entered name as the custom PlayerId to check into SocketWeaver services, and we will reused the entered name as the custom string data to register the player to the Lobby server.

Make sure the custom playerId is unique. You might want to set it to the objectID of the player in your user profile database. In this demo project, we are just going to manually enter unique names for the players because we don't have a user profile database.

Open the LobbyDemo.cs script and update the RegisterPlayer() method.

    public void RegisterPlayer()
    {
        GUI.ShowRegisterPlayerPopup((bool ok, string playerName) =>
        {
            if (ok)
            {
                // store the playerName
                // playerName also used to register local player to the lobby server
                playerName_ = playerName;
                NetworkClient.Instance.CheckIn(playerName, (bool successful, string error) =>
                {
                    if (!successful)
                    {
                        Debug.LogError(error);
                    }
                });
            }
        });
    }

Adding Lobby Connected Event handler

void Start()
{
    // Subscribe to Lobby events
    NetworkClient.Lobby.OnNewPlayerJoinRoomEvent += Lobby_OnNewPlayerJoinRoomEvent;
    NetworkClient.Lobby.OnPlayerLeaveRoomEvent += Lobby_OnPlayerLeaveRoomEvent;
    NetworkClient.Lobby.OnRoomCustomDataChangeEvent += Lobby_OnRoomCustomDataChangeEvent;

    NetworkClient.Lobby.OnRoomMessageEvent += Lobby_OnRoomMessageEvent;
    NetworkClient.Lobby.OnPlayerMessageEvent += Lobby_OnPlayerMessageEvent;

    NetworkClient.Lobby.OnLobbyConncetedEvent += Lobby_OnLobbyConncetedEvent;
}

void onDestroy()
{
    // Unsubscrible to Lobby events
    NetworkClient.Lobby.OnNewPlayerJoinRoomEvent -= Lobby_OnNewPlayerJoinRoomEvent;
    NetworkClient.Lobby.OnPlayerLeaveRoomEvent -= Lobby_OnPlayerLeaveRoomEvent;
    NetworkClient.Lobby.OnRoomCustomDataChangeEvent -= Lobby_OnRoomCustomDataChangeEvent;

    NetworkClient.Lobby.OnRoomMessageEvent -= Lobby_OnRoomMessageEvent;
    NetworkClient.Lobby.OnPlayerMessageEvent -= Lobby_OnPlayerMessageEvent;

    NetworkClient.Lobby.OnLobbyConncetedEvent -= Lobby_OnLobbyConncetedEvent;
}

Registering Player

You can use SWLobby's member method Register(string, Action<bool, SWRegisterReply, SWLobbyError>) to register the player.

Open the LobbyDemo.cs script and implement the Lobby_OnLobbyConncetedEvent() method.

void Lobby_OnLobbyConncetedEvent()
{
    Debug.Log("Lobby_OnLobbyConncetedEvent");
    // Register the player using the entered player name.
    NetworkClient.Lobby.Register(playerName_, (successful, reply, error) =>
    {
        if (successful)
        {
            Debug.Log("Lobby registered " + reply);
            if (reply.started)
            {
                // Player is in a room and the room has started.
                // Call NetworkClient.Instance.ConnectToRoom to connect to the game servers of the room.
            }
            else if (reply.roomId != null)
            {
                // Player is in a room.
                GetRooms();
                GetPlayersInCurrentRoom();
            }
            else
            {
                // Player is not in a room.
                GetRooms();
            }
        }
        else
        {
            Debug.Log("Lobby failed to register " + error);
        }
    });
}

Last updated