Handling Disconnects

EricSEricS
Hello. Our game has the following flow for determining if all players ( between 5 and 8 ) are ready:

When each player is ready, they will set a custom property indicating they are ready and the call an rpc which tells all clients to update their loading ui and the master increments a counter of how many players are ready. The master then checks if all players are ready.

here is the code:
private void SetupComplete()
    {
        ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();
        playerProperties.Add(Strings.PLAYER_LOAD_STATUS, true);
        PhotonNetwork.player.SetCustomProperties(playerProperties);
        photonView.RPC("RemoteCallForSetupComplete", PhotonTargets.AllBufferedViaServer);
    }

 [PunRPC]
    public void RemoteCallForSetupComplete()
    {
        //add call here to update player loading tabs
        if (LoadingScreen.GetInstance() != null)
        {
            LoadingScreen.GetInstance().UpdatePlayerLoadingTabs();
        }
        if (PhotonNetwork.isMasterClient)
        {
            ++playerSyncCount;
            if (PhotonNetwork.offlineMode || (playerSyncCount >= PhotonNetwork.playerList.Length))
                photonView.RPC("SetupCompleteForEveryOne", PhotonTargets.AllBufferedViaServer);
        }
}

This code works fine. However we need to handle the situation where a player disconnects. To do that we use the OnPhotonPlayerDisconnected callback. From OnPhotonPlayerDisconnected, the master client calls the following function to check if all players are now ready as the disconnecting player may have been the only one we were waiting for:
public void MasterCheckSetupCompleteAfterPlayerDisconnect()
    {

        if (LoadingScreen.GetInstance() != null)
        {
            LoadingScreen.GetInstance().UpdatePlayerLoadingTabs();
        }

        Debug.LogError("Player count : " + playerSyncCount + " actual count " + PhotonNetwork.playerList.Length);

        if (PhotonNetwork.offlineMode || (playerSyncCount >= PhotonNetwork.playerList.Length))
            photonView.RPC("SetupCompleteForEveryOne", PhotonTargets.AllBufferedViaServer);
    }

Here is the issue. What if the player who disconnected has already completed setup? In this case I should decrement the playerSyncCount. The issue is I am not 100% sure what the best way to do that is.

Here are possibilities

1) I check the Strings.PLAYER_LOAD_STATUS for the disconnecting player to see if it is true. This seems like a good idea. I just wonder if there might be an edge case where this won't have updated right after the player disconnects?

2) Modify the code, so the master client doesn't rely on an rpc call to indicate players are done loading, and rather periodically check the player properties hash table to compare number of players with loaded players

Please advise

Best Answer

Answers

Sign In or Register to comment.