Hello, I have a simple lobby system. When a player in lobby click the ready button, all players must see that he is ready. I’m doing that by subscribing PlayerDataChanged event callback and changing “ready” data in Player object. Sometimes even if I change ready data, PlayerDataChanged event is not triggered. I’ve noticed that Unity’s official Lobby sample project has the same issue. Is there a solution for this problem ?
Here’s the code I use to reflect ready data to UI
private async void SubscribeLobbyEvents()
{
_lobbyEventCallbacks = new LobbyEventCallbacks();
_lobbyEventCallbacks.LobbyChanged += OnLobbyChanged;
_lobbyEventCallbacks.KickedFromLobby += OnKickedFromLobby;
_lobbyEventCallbacks.PlayerDataChanged += OnPlayerDataChanged;
try
{
await Lobbies.Instance.SubscribeToLobbyEventsAsync(JoinedLobby.Id, _lobbyEventCallbacks);
}
catch (LobbyServiceException e)
{
switch (e.Reason)
{
case LobbyExceptionReason.AlreadySubscribedToLobby: Debug.LogWarning($"Already subscribed to lobby[{JoinedLobby.Id}]. We did not need to try and subscribe again. Exception Message: {e.Message}"); break;
case LobbyExceptionReason.SubscriptionToLobbyLostWhileBusy: Debug.LogError($"Subscription to lobby events was lost while it was busy trying to subscribe. Exception Message: {e.Message}"); throw;
case LobbyExceptionReason.LobbyEventServiceConnectionError: Debug.LogError($"Failed to connect to lobby events. Exception Message: {e.Message}"); throw;
}
}
}
private void OnPlayerDataChanged(Dictionary<int, Dictionary<string, ChangedOrRemovedLobbyValue<PlayerDataObject>>> obj)
{
Debug.Log("player data changed");
OnLobbyPlayersUpdateRequested?.Invoke();
}
private void UpdateLobbyPlayers()
{
for (int i = 0; i < lobbyUserControllers.Length; i++)
{
if (i >= _currentLobby.Players.Count || _currentLobby.Players[i] == null) lobbyUserControllers[i].ResetUser();
else
{
var isUserHost = IsPlayerHost(_currentLobby.Players[i].Id);
lobbyUserControllers[i].ChangeUserType(_isOwnerHost, isUserHost);
lobbyUserControllers[i].UpdateUser(_currentLobby.Players[i]);
}
}
}
public void UpdateUser(Player player)
{
_currentPlayer = player;
playerNameText.text = player.Data[LobbyManager.Instance.PlayerNameKey].Value;
readyIcon.SetActive(player.Data[LobbyManager.Instance.PlayerReadyKey].Value == "true");
kickButton.onClick.AddListener(KickClickHandler);
}