Best Practice for Leaving and Rejoining an existing room

Hi,

I am kinda new to Photon at this point and am using PUN2.

I have an existing setup that works fine for anyone waiting to join or joining rooms. However, when a client leaves a room, with other clients still in the room, and tries to rejoin, things start acting strange.

I'm not sure if there is a best practice or sample out there that can help me understand what's going on, but it seems that the client that joins a room for the second time isn't Locally controlled, an rpc that is setup to spawn the rejoined client doesn't get called and it seems isMine is no longer true.

Any advice would be greatly appreciated!

Comments

  • emotitron
    emotitron ✭✭✭
    edited July 2020

    Are you using rejoin? Or are you reconnecting from scratch? Be sure to pull the latest version 2.19.2 from the store as well, as there were some changes to that.

  • So initially, i'm just trying to reconnect from scratch.
    My current flow is as follows:
    ConnectUsingSettings()
    OnConnectedToMaster() -> Get list of available rooms
    then either JoinRoom(existingRoom) or CreateRoom(newRoom,RoomOptions).

    When the application closes there's logic to leave the room and disconnect.

    There are a few things that I'm not sure of, how can I determine whether a client needs to reconnect vs connect and/or rejoin vs join?
    Or how can I setup a room to enable the same user to join and rejoin?

    I believe I have the latest as well.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited July 2020
    Hi @theophysist,

    Thank you for choosing Photon!

    Reconnect ==> reconnect after unexpected disconnect.
    ReconnectAndRejoin ==> quick rejoin: reconnect and rejoin a room after unexpected disconnect while joined to a room.
    RejoinRoom ==> rejoin a room using same UserID. an actor with the same UserID is expected to be inactive in the room's actors list. if the room does not exist on Photon Servers, it can be loaded from an external source if this is configured/enabled.

    If JoinOrCreateRoom or JoinRoom fails with some error codes/message indicating that a user with the same UserID exists, you may try RejoinRoom instead.

    A lot of factors intervene here: PlayerTtl, EmptyRoomTtl, LeaveRoom(willComeBack?), Disconnect(), DisconnectCause, etc.
  • Hi @JohnTube

    Thanks! That helps a lot. I didn't realize the error code about the same UserID exists.

    There's another issue I've run into relating to "rejoining", (not actually calling Rejoin(), when the user joins the room the first time, everything works fine as I have an RPC that gets called to instantiate the local player. However, when that same player joins the room again, that RPC doesn't get called and that player prefab doesn't get instantiated. Is this the same issue?
    The scene gets loaded but no PhotonViews are registered (according to the logs) for the re-connecting user.
  • emotitron
    emotitron ✭✭✭
    If you completely leave a game (called a Hard Disconnect), and then Join that game, all previous cached RPCs (which includes instantiations) get removed by the relay.

    Your auto-spawn code for the local player should be called in OnJoinedRoom(), and you can check PhotonNetwork.LocalPlayer.HasRejoined == false to see if you need to execute your auto-spawn code. If I understand your question correctly.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hey @theophysist,

    are those RPCs buffered?
  • theophysist
    edited July 2020
    @emotitron sorry for the delay, Thank you so much! I believe that is the issue. I'll need to move some code around but that clears up a lot.