How to Make a Lobby

A lobby is a room used to hold players while they wait to join a game. To create a room with the Strix Unity SDK the CreateRoom() method is used. The following code shows how to create a room with some properties to make it useful as a lobby.

StrixNetwork.instance.CreateRoom(
    new RoomProperties {
        name = "New Room",
        capacity = 4,
        key1 = 0            // Initial average skill level is 0
    },
    new RoomMemberProperties {
        name = "Player name",
        properties = new Dictionary<string, object>() {
            { "state", 0 }  // Initial state is "Not Ready"
        }
    },
    args => {
        Debug.Log("CreateRoom succeeded");
    },
    args => {
        Debug.Log("CreateRoom failed. error = " + args.cause);
    }
);

The important properties for a lobby in this example are key1 and state.

key1 is the average skill level of the room members. state holds the ready state of a given member (0 for not ready, 1 for ready).

Note

Any values can be used but remember that custom properties cannot be searched. If you want this information to be available to someone outside the room, you will want to use one of the key values.

Before creating a room, connection to a master server should be established. A client automatically joins the created room after it is successfully created. The first client is given room owner authority, i.e., it is given authority to update the room information, kick other members, and so on.

When a room member is ready for the game its state should be set to the ready state. For example:

// Change the room member state: 0 - preparation, 1 - ready
StrixNetwork.instance.SetRoomMember(
    StrixNetwork.instance.selfRoomMember.GetPrimaryKey(),
    new Dictionary<string, object>() {
        { "properties", new Dictionary<string, object>() {
            { "state", state }
        } }
    },
    args => {
        Debug.Log("SetRoomMember succeeded");
    },
    args => {
        Debug.Log("SetRoomMember failed. error = " + args.cause);
    }
);

Here selfRoomMember is a CustomizableMatchRoomMember instance of the current client which is connected to the server. It is initialized after joining a room. In this example a room member can accept 0 and 1 as a state property. In your application you can use any other values or another property name.

Note

SetRoomMember method requests the server to update member property values based on the Dictionary passed as the argument. Properties not specified in the Dictionary are unchanged. The same is true for the set of custom properties stored as the value for the “properties” key of the outer dictionary. That is, if you had multiple custom properties, values of custom properties other than “state” would be unchanged by the above code.

It is a good idea to wait for all members including the owner to be ready before beginning the game (see How to Wait for All Members to be Ready for more information).

The game requires that other members join the room. A member can join the room using the JoinRoom() method.

StrixNetwork.instance.JoinRoom(
    new RoomJoinArgs {
        host = "127.0.0.1",
        port = 9123,
        protocol = "TCP",
        roomId = 1
    },
    args => {
        Debug.Log("JoinRoom succeeded");
    },
    args => {
        Debug.Log("JoinRoom failed. error = " + args.cause);
    }
);