How to change a room?

Hello there,
i'm searching for the recommended way to switch a room. So i already tryed out some things and got a result i'm not really happy with. I create a room when i join the game. Then i want to switch into another scene and create a new room with a properties room name, after joining the new room the other players still can see my character in the old one.

Here is my setup:

Joining into the world for the first time:
public class NetManager : Photon.MonoBehaviour
{
public virtual void Start()
{
PhotonNetwork.ConnectUsingSettings("v1.0");
}

public virtual void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
PhotonNetwork.JoinRandomRoom();
}

public virtual void OnJoinedLobby()
{
Debug.Log("OnJoinedLobby(). This client is connected and does get a room-list, which gets stored as PhotonNetwork.GetRoomList(). This script now calls: PhotonNetwork.JoinRandomRoom();");
PhotonNetwork.JoinRandomRoom();
}

public virtual void OnPhotonRandomJoinFailed()
{
Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 4 }, null);
}

public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
{
Debug.LogError("Cause: " + cause);
}

public void OnJoinedRoom()
{
Debug.Log("OnJoinedRoom() called by PUN. Now this client is in a room. From here on, your game would be running. For reference, all callbacks are listed in enum: PhotonNetworkingMessage");

if(GameObject.FindGameObjectWithTag("Player") == null) // Prevent from spawning the player again (because it's not getting destroyed on Load)
{
GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
}

// Create the player head
CreatePlayer();
}
And here is the script that gets called after switching into the new scene:

public class NetManagerArena : Photon.MonoBehaviour
{
public virtual void Start()
{
PhotonNetwork.ConnectUsingSettings("v1.0");
}

public virtual void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster() was called by PUN. No arena room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 2}, null);");
PhotonNetwork.JoinOrCreateRoom("arena", new RoomOptions() { MaxPlayers = 2 }, null);
}

public virtual void OnJoinedLobby()
{
PhotonNetwork.JoinRoom("arena");
}

public virtual void OnPhotonRandomJoinFailed()
{
Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 2}, null);");
PhotonNetwork.CreateRoom("arena", new RoomOptions() { MaxPlayers = 2 }, null);
}

public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
{
Debug.LogError("Cause: " + cause);
}

public void OnJoinedRoom()
{
Debug.Log("OnJoinedRoom() called by PUN. Now this client is in a room. From here on, your game would be running. For reference, all callbacks are listed in enum: PhotonNetworkingMessage");

CreatePlayer();

}
How the scene change gets called:
private void ChangeScene()
{
PhotonNetwork.DestroyPlayerObjects(PhotonNetwork.player);
PhotonNetwork.LeaveRoom();
PhotonNetwork.Disconnect();
SceneManager.LoadScene("Arena");
}

Comments

  • Hi,

    I think you are getting confused with the concept of rooms and scenes/levels, maybe not, but let me clarify things first:

    it's perfectly fine to stay in the same room and load various levels. A room is only a mean for s^players to be together, so if you are not changing players, not point leaving a room to join another one.

    now, leaving and joining a room is a asynchrone process and it may even not realize, this is probably the biggest issue here, if you want your scene to reflect your room you have to wait for Photon to tell you you are in a room to load the scene. You can also load the scene and then wait for joining the room but you will need a way to visually indicates what's going on, I think this is missing for you here.

    the fact that you can still see your old character is odd indeed, are you calling LeaveRoom for your player?

    Bye,

    Jean
  • JannickL
    JannickL
    edited November 2016
    Hi Jean,
    i want to leave the room (which allows like 4 - 16 players) to join the arena that allows only two players.
    I thought that what rooms are for?

    Yeah leaveRoom gets called. The system is working fine for me (expect the bug with the visibility). Just want to know if i'm using the rooms as it's meant to be.

    Thx
  • Hi,

    then yes, you are using rooms properly.

    your character being still visible is likely because you turned off autoCleanup, can you double check on that? also do you have your playr set to no destroyonload or something? or do you play with the photonView Component like disabling it or else?

    Bye,

    Jean
  • Did you ever solve this problem?