How keep everything the same on disconnect?

I set RoomOptions.CleanupCacheOnLeave to false but all the instantiated objects seem to revert to how they where instantiated when my client disconnects (and reconnects).

I would like everything to stay the same as my game is a turn-based strategy game and if a player reconnects they should be able to continue with their units in the correct state.

Is there any way to stop anything from changing on disconnect since AutoCleanUp = false doesn't seem to do the job?

Best Answers

  • Tobias
    Tobias admin
    Answer ✓

    CleanupCacheOnLeave makes sure the objects don't get destroyed and removed from the server cache.

    When you rejoin, your client gets all Instantiate events again and this resets the objects.

    I think there is no way to prevent this except by modifying PUN itself. You could change the PhotonView registration code to keep the old objects instead of the new ones (the old get destroyed by default).

    You may also change the scripts that are observed by PhotonViews. You could hide instantiated objects until they get their first state update (after joining).

  • TDP
    TDP
    Answer ✓

    For anyone finding this post in the future I'd like to record how I managed to keep everything the same on disconnect and reconnect. There are two sections in the Photon code that need to be adjusted.

    First, navigate to "Assets/Photon/PhotonUnityNetworking/Code". Open the "PhotonHandler" script and on line 320 set the boolean parameter for PhotonNetwork.LocalCleanupAnythingInstantiated to false.

    public void OnLeftRoom()
    {
        // Destroy spawned objects and reset scene objects
        PhotonNetwork.LocalCleanupAnythingInstantiated(false);
        Debug.Log("not cleaning up objects on disconnect");
    }
    

    This keeps all of a players objects intact on all clients when that player disconnects.

    Second, in the same folder open the script "PhotonNetwork" and add this code at around line 2586, which should be at the beginning of the NetworkInstantiate() method.

    // cancel instantiate if viewId exists already
    if (parameters.viewIDs != null && photonViewList.TryGetValue(parameters.viewIDs[0], out PhotonView listedView))
    {
        Debug.Log(parameters.prefabName + " instantiate cancelled : viewId already exists");
        return null;
    }
    

    This causes the objects owned by the disconnected player not to reinstantiate when that player reconnects and did not clean them up on leaving.

    All of this works if the disconnecting player obviously stays in the same scene and does not delete their objects in any other way. It has been working for me in my game and not having to restart a session every time someone disconnects saves a great deal of hassle.

Answers

  • Tobias
    Tobias admin
    Answer ✓

    CleanupCacheOnLeave makes sure the objects don't get destroyed and removed from the server cache.

    When you rejoin, your client gets all Instantiate events again and this resets the objects.

    I think there is no way to prevent this except by modifying PUN itself. You could change the PhotonView registration code to keep the old objects instead of the new ones (the old get destroyed by default).

    You may also change the scripts that are observed by PhotonViews. You could hide instantiated objects until they get their first state update (after joining).

  • TDP
    TDP
    Answer ✓

    For anyone finding this post in the future I'd like to record how I managed to keep everything the same on disconnect and reconnect. There are two sections in the Photon code that need to be adjusted.

    First, navigate to "Assets/Photon/PhotonUnityNetworking/Code". Open the "PhotonHandler" script and on line 320 set the boolean parameter for PhotonNetwork.LocalCleanupAnythingInstantiated to false.

    public void OnLeftRoom()
    {
        // Destroy spawned objects and reset scene objects
        PhotonNetwork.LocalCleanupAnythingInstantiated(false);
        Debug.Log("not cleaning up objects on disconnect");
    }
    

    This keeps all of a players objects intact on all clients when that player disconnects.

    Second, in the same folder open the script "PhotonNetwork" and add this code at around line 2586, which should be at the beginning of the NetworkInstantiate() method.

    // cancel instantiate if viewId exists already
    if (parameters.viewIDs != null && photonViewList.TryGetValue(parameters.viewIDs[0], out PhotonView listedView))
    {
        Debug.Log(parameters.prefabName + " instantiate cancelled : viewId already exists");
        return null;
    }
    

    This causes the objects owned by the disconnected player not to reinstantiate when that player reconnects and did not clean them up on leaving.

    All of this works if the disconnecting player obviously stays in the same scene and does not delete their objects in any other way. It has been working for me in my game and not having to restart a session every time someone disconnects saves a great deal of hassle.

  • Glad you could make such a modification work. Good job. Thanks for sharing!