Player reconnects to the game, player does not spawn at the last location.

Options
When the connection is lost, the player is destroyed from the game scene.
I don't want the player to destroy when the connection is lost and when the player reconnects to the game, player does not spawn at the last location.
giphy.gif



PhotonNetwork.ReconnectAndRejoin();
I am reconnecting to the game with this code.

options.CleanupCacheOnLeave = false;
options.PlayerTtl = 50000;
options.EmptyRoomTtl = 50000;
some of the settings of the room

Best Answer

  • Tobias
    Tobias admin
    Answer ✓
    Options
    This is kind of tricky but .. I guess it's possible to get there.

    To avoid the destruction of the objects when you drop out of a room, you need to modify the code a bit.
    In LoadBalancingClient, line 3022, add this code where wasInRoom is set:
    bool wasInRoom = this.CurrentRoom != null;
    if (this.State == ClientState.Joined)
    {
        // special case: when the client is in a room (joined) but directly disconnects, set a Disconnecting State (this triggers a StateChanged callback).
        // the callback helps higher level apis to clean up before the room is gone (by reacting to StateChanged from Joined to Disconnecting).
        this.State = ClientState.Disconnecting;
    }
    

    Also, you have to empty PhotonHandler.OnLeftRoom(). It should no longer call LocalCleanupAnythingInstantiated.

    These changes may make sense for PUN 2 in general and may end up in v2.31. This is not entirely clear yet. Also, I don't know if PUN will seamlessly merge the objects on Re-Join, if you keep them after disconnecting. I can not dive into this right now, so consider this unsupported for now.


    Instantiation will always happen where it was originally placed.
    Your scripts on the object can hide it until it was placed somewhere with a network update (if the object is remote).


    Hope this helps somewhat. Sorry for the vague-ish reply. Changing parts of the state transitions is always tricky as it can break a lot of workflows.

Answers

  • Tobias
    Tobias admin
    Answer ✓
    Options
    This is kind of tricky but .. I guess it's possible to get there.

    To avoid the destruction of the objects when you drop out of a room, you need to modify the code a bit.
    In LoadBalancingClient, line 3022, add this code where wasInRoom is set:
    bool wasInRoom = this.CurrentRoom != null;
    if (this.State == ClientState.Joined)
    {
        // special case: when the client is in a room (joined) but directly disconnects, set a Disconnecting State (this triggers a StateChanged callback).
        // the callback helps higher level apis to clean up before the room is gone (by reacting to StateChanged from Joined to Disconnecting).
        this.State = ClientState.Disconnecting;
    }
    

    Also, you have to empty PhotonHandler.OnLeftRoom(). It should no longer call LocalCleanupAnythingInstantiated.

    These changes may make sense for PUN 2 in general and may end up in v2.31. This is not entirely clear yet. Also, I don't know if PUN will seamlessly merge the objects on Re-Join, if you keep them after disconnecting. I can not dive into this right now, so consider this unsupported for now.


    Instantiation will always happen where it was originally placed.
    Your scripts on the object can hide it until it was placed somewhere with a network update (if the object is remote).


    Hope this helps somewhat. Sorry for the vague-ish reply. Changing parts of the state transitions is always tricky as it can break a lot of workflows.
  • Tobias
    Options
    By the way: This is the wrong subforum for the question.
    Use the Photon Unity Networking forum :)
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Hunel,

    Thank you for choosing Photon!

    Your game looks cool from the little GIF.
    I will move this discussion to PUN category from Server category.

    Please let us know the outcome when you try the solution suggested by Tobias.
    Thanks.
  • Hunel
    Hunel
    edited April 2021
    Options
    @Tobias , @JohnTube
    Sorry for asking a question on the wrong sub forum.

    bool wasInRoom = this.CurrentRoom != null;
    if (this.State == ClientState.Joined)
    {
        // special case: when the client is in a room (joined) but directly disconnects, set a Disconnecting State (this triggers a StateChanged callback).
        // the callback helps higher level apis to clean up before the room is gone (by reacting to StateChanged from Joined to Disconnecting).
        this.State = ClientState.Disconnecting;
    }
    
    LoadBalancingClient, at line 3022, I added this code where wasInRoom is set.

    I have emptied PhotonHandler.OnLeftRoom ().
    LocalCleanupAnythingInstantiated is no longer called.

    As I wanted, the player doesn't destroy when the connection is lost but when reconnected, the player destroyed.
    giphy.gif
    I looked for a solution for this and found it.
    PhotonNetworkPart,I deleted the code on line 1014.
    RemoveInstantiatedGO(listedView.gameObject, true);
    

    When the connection is lost, the player does not destroyed and the player does not destroyed when reconnected.
    giphy.gif
    Everything seems to be working normally but something is wrong.

    when the player reconnects to the room, all previously spawned objects are spawned again.

    I use the code "PhotonNetwork.Instantiate" to spawn objects(I guess it works as allbuffered).
    to solve this error I destroyed every object spawned with PhotonNetwork.Instantiate code(with PhotonNetwork.Destroy)

    To fix some minor bugs I have edited the line 1000-2025 like this(PhotonNetworkPart)
    Ads-z2.png

    I tested it for about 30-50 minutes and everything is working fine but there is another little problem.
    Reconnect & ReconnectAndRejoin works fine with pun but does not work properly with photon server.
    What is the reason for this?

    and

    How do I check when someone has connected / disconnected?









  • JohnTube
    JohnTube ✭✭✭✭✭
    edited April 2021
    Options
    Very good job on your tests, findings and suggested changes.
    Your feedback is valuable.
    But we need to wait for Tobias here.
    Reconnect & ReconnectAndRejoin works fine with pun but does not work properly with photon server.
    Yes this is a known issue in v4 that will be fixed in v5 (that will be released soon in May), read here:
    Ther server does not fully support quick rejoin feature (ReconnectAndRejoin) as it does not implement session tokens.
    ---
    How do I check when someone has connected / disconnected?
    What do you mean? from PUN/client side or server side?

    Thanks again for your efforts and time.
  • Tobias
    Options
    Sorry for asking a question on the wrong sub forum.

    No worries. It mostly matters for the visibility of the question...
    when the player reconnects to the room, all previously spawned objects are spawned again.

    I use the code "PhotonNetwork.Instantiate" to spawn objects (I guess it works as allbuffered).
    to solve this error I destroyed every object spawned with PhotonNetwork.Instantiate code(with PhotonNetwork.Destroy)

    When you set CleanupCacheOnLeave to false, this should have stopped removing the objects (with the changes we did above).
    When you come back, yes, PUN would instantiate again (getting the buffered messages) and replace the old with the new instances. This you disabled by commenting out the code.

    It should be possible to check if a viewID exists locally and if so, skip instantiation.
    However: As long as it works, it's fine.

    I am not sure if I want to introduce these changes to PUN 2 when it's in use in so many projects. This can be quite a breaking change. So please keep track of these changes and please keep on updating despite having to merge this in.

    How do I check when someone has connected / disconnected?

    On the client, you get a OnPlayerEnteredRoom(Player newPlayer) callback. Check newPlayer.HasRejoined.
    When a player leaves, check IsInactive.