Search Unity

Third Party Photon PUN - respawning objects on client when restarted after a crash (hard disconnect)

Discussion in 'Multiplayer' started by Sound-Master, Mar 23, 2021.

  1. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    48
    Hello!

    I am using Unity 2019.3.4 and Photon PUN 2 version 2.26.

    I have a very simple scenario with only one room all players connect to. I have set
    Code (CSharp):
    1. PhotonNetwork.AutomaticallySyncScene = true;
    So that when th emaster client calls
    Code (CSharp):
    1. PhotonNetwork.LoadLevel("MyScene");
    all other clients load the new level too.

    Each client spawns a player GameObject when they connect. The master client drops an additional GameObject in each scene, calling
    Code (CSharp):
    1. PhotonNetwork.Instantiate()
    and at the right time loads a new scene for all clients. There only three scenes.

    I am assuming that everytime the master client loads a new scene, all networked objects previously instantiated with
    Code (CSharp):
    1. PhotonNetwork.Instantiate()
    are destroyed and it seems to be the case.

    So far so good.

    However, if a client (not the master) crashes, when I restart it, it joins the room and loads the right scene (because of
    Code (CSharp):
    1. PhotonNetwork.AutomaticallySyncScene = true;
    ) but it also locally re-spawns all networked objects which were created in previous scenes!

    Why does that happen? I thought all networked objects spawned in a scene were destroyed when a new scene is loaded. is that not the case?

    The only way I found to avoid this issue is to call
    Code (CSharp):
    1. PhotonNetwork.DestroyAll();
    before loading a new scene. Is that what I am supposed to do?

    @tobiass

    Many thanks

    Michele
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    Sorry for the late reply.

    When you join a room, the server will repeat all instantiation calls which were not removed via PhotonNetwork.Destroy(). This is what you see: The client will re-instantiate everything but won't destroy objects from earlier scenes (which clients locally deleted when they switched).
    In many cases, this does not make a difference but yes, for rejoin this is problematic. We will try to look into this.

    A normal Destroy (as done by the engine on load) will not clean up the server's instantiation list.
    Using DestroyAll will tell the server you want the old instantiates gone, so this is OK to use before load.
     
    Sound-Master likes this.
  3. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    48
    Thank you so much for answering, @tobiass! I really appreciate it. It is exactly the rejoin after a hard reset which is an issue in my case, as all gameobjects spawned in previous scenes are re-spwned locally only on the rejoining machine, even if they had been originally spawned in prevoius and no longer active scenes.

    Yes, using DestroyAll works for me, thanks for confirming. It would be an issue though if I had a gameobject or RPC I needed to keep alive across different scenes. Luckily for me this is not the case but I suspect it would be an issue only in very specific cases.

    Many thanks!