How to Wait for All Members to be Ready

To let the owner choose when to begin the game it is necessary to wait for all members including the owner to be ready.

A room member’s status can be stored in a Room Member object (see How to Make a Lobby for more information).

To check the state of all members the following code can be used.

public static bool CheckAllRoomMembersState(int desiredState) {
    foreach (var roomMember in StrixNetwork.instance.roomMembers) {
        if (!roomMember.Value.GetProperties().TryGetValue("state",
            out object value)) {
            return false;
        }

        if ((int) value != desiredState) {
            return false;
        }
    }

    Debug.Log("CheckAllRoomMembersState OK");
    return true;
}

Here StrixNetwork.instance.roomMembers is a container which stores all members’ instances in the room. The function checks if the state of all members is equal to the desiredState argument.

CheckAllRoomMembersState() can be called in two ways.

Firstly, it can be called within a coroutine using the WaitUntil class. The following code demonstrates this approach.

// Wait for all members and start the game
void Start() {
    StartCoroutine(WaitStartGame());
}

private IEnumerator WaitStartGame() {
    yield return new WaitUntil(() => CheckAllRoomMembersState(1));
}

Another approach is to use RoomSetMemberNotification. For that it is necessary to register a RoomSetMemberNotified event handler. The following code demonstrates this approach.

public static void AddRoomSetMemberHandler() {
    StrixNetwork.instance.roomSession.roomClient.RoomSetMemberNotified +=
        RoomSetMemberNotifiedHandler;
}

private static void RoomSetMemberNotifiedHandler(
    NotificationEventArgs<RoomSetMemberNotification<CustomizableMatchRoomMember>> notification) {
    if (CheckAllRoomMembersState(1) {
        // All members are ready, start the game
        StartGame();
    }
}

Note

The contents of StrixNetwork.instance.roomMembers will be updated automatically to reflect changes requested by other clients, not just that new members are added and/or left members are removed, but even a CustomizableMatchRoomMember object for a same member may be recreated time-to-time, as the underlying network operations proceed.

That means you can’t cache objects got from roomMembers for use in future frames; you always need to get fresh objects from roomMembers every frame (or every time a new notification arrives), as CheckAllRoomMembersState in the above example does.