Best Practice for Leaving and Rejoining an existing room

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Best Practice for Leaving and Rejoining an existing room

theophysist
2020-07-02 21:11:49

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
2020-07-02 23:07:25

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.

theophysist
2020-07-03 10:28:57

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
2020-07-03 15:49:04

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.

theophysist
2020-07-04 11:02:31

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
2020-07-06 10:54:02

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
2020-07-06 11:17:49

Hey @theophysist,

are those RPCs buffered?

theophysist
2020-07-07 20:37:55

@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.

Back to top