Moving Client between rooms

Options
Hi guys,

Out of interest - what's the recommended workflow for moving a player between rooms? For instance, we have a player who has connected to: "Room1"

Due to some imaginary reason, we want to move the player out of "Room1" and in to "Room2"

Thus far I've come to understand, you cannot simply hop between rooms. You must first issue:

PhotonNetwork.LeaveRoom();

What I've got so far is:
(Sorry for the horrible nesting, this is test code)

[code2=csharp]private string queuedroom = "";
void Update()
{
if (PhotonNetwork.connectionState == ConnectionState.Connected)
{
if (queuedroom != "")
{
if (PhotonNetwork.room != null)
{
PhotonNetwork.LeaveRoom();

}
else
{
RoomInfo[] rooms = PhotonNetwork.GetRoomList();
bool lbFoundRoom = false;
for (int i = 0; i < rooms.Length; i++)
{
if (rooms.name == queuedroom)
{
PhotonNetwork.JoinRoom(rooms);
lbFoundRoom = true;
queuedroom = "";
break;
}
}
if (!lbFoundRoom)
{
PhotonNetwork.CreateRoom(queuedroom, true, true, 10);
queuedroom = "";
}
}
}

if (Input.GetKeyDown(KeyCode.F1))
{
queuedroom = "phase1";
}
else if (Input.GetKeyDown(KeyCode.F2))
{
queuedroom = "phase2";
}
}
}[/code2]

However this throws the error:
createGame failed, client stays on masterserver: OperationResponse 227: ReturnCode -3 (Not authorized). Parameters: {}.

What am I missing here? An educated guess would tell me that leaving the room actually causes the client to reauthenticate with the photon server/cloud and at this point I'm perhaps trying to rejoin a room too quickly. Although I thought that would've been caught by the if (PhotonNetwork.ConnectionState == Connected) clause preventing re-entry on the next loop until Photon has fully re-established?

Thanks,
Steve

Comments

  • Tobias
    Options
    Thus far I've come to understand, you cannot simply hop between rooms.
    This is because the rooms are distributed across many machines and the master server has to send you the game server's address to join a particular room.

    The error you get is due to the order of the operations you call. The client does not only need to connect but before it can call operations, it has to authenticate itself. This is done behind the scenes in the loadbalancingclient but your code does not wait until that's done. You should implement the callback methods defined in PhotonNetworkingMessage. If you try to join the room you want to switch to in OnJoinedLobby(), you will succeed.
  • Thank you, that helps it make a lot more sense :)

    I'd temporarily hacked around the issue by issuing an arbitrary wait clause, but callbacks are a far far nicer way of going about this.