Simplest question (player leaving room)

Options
Zido
Zido
Hi all! I'm starting with PUN and I have a doubt.
I downloaded the viking example for see the code but I'm not so clear yet.
I made a simple system for join rooms, players can join and everything works fine, but when i push "leave & quit" button ( this is totally copied from the viking example ) locally the player exits the room but in the other end the player gameobject remains there, what i should do, destroy the player gameobject in some callback? I thought this was automated same as if you join room and if there are some other players it auto-instantiates them.

thanks for your help :D

Comments

  • Tobias
    Options
    Please have a look at what Viking Demo does. Leave & Quit will call leave, which trigger a workflow that's handled by PUN. It disconnects you from the game server, connects to the master server (again) and then you're back in the lobby where you can join other games. Or you call disconnect on the other hand, this will disconnect you for good. You might connect again.
  • Zido
    Options
    I did exactly that but for some reason my code isn't working, here is the whole script simplified, still doesn't work.
    using UnityEngine;
    using System.Collections;
    public class Init : Photon.MonoBehaviour {
    
    	// Use this for initialization
    	void Start () {
    		PhotonNetwork.ConnectUsingSettings("v1.0"); 
    		PhotonNetwork.player.name = "Jugador"+Random.Range(0,999999);
    	}
    	
    	void OnJoinedRoom()
    	{
    		Debug.Log("Room: "+PhotonNetwork.room.name);
    		GameObject monster = PhotonNetwork.Instantiate("Panda/Panda",Vector3.zero,Quaternion.identity,0);
    		monster.name = "Player"+photonView.viewID.ID;
    	}
    	IEnumerator OnLeftRoom()
        {
            while(PhotonNetwork.room!=null || PhotonNetwork.connected==false)
                yield return 0;
            Application.LoadLevel(Application.loadedLevel);
        }
    	void OnPhotonRandomJoinFailed() 
    	{
    		PhotonNetwork.CreateRoom("1");
    		PhotonNetwork.JoinRoom("1");
    	}
    	void OnPhotonJoinRoomFailed()
    	{
    		PhotonNetwork.JoinRandomRoom();
    	}
    	void OnGUI()
        {
            if (PhotonNetwork.room == null)
    		{
    	        if (GUILayout.Button("Join random room"))
    	        {
    	            PhotonNetwork.JoinRandomRoom();
    	        }
    		}
    		else
    		{
    	        if (GUILayout.Button("Leave& QUIT"))
    	        {
    	            PhotonNetwork.LeaveRoom();
    	        }
    		}
        }
    }
    

    when i push Leave& QUIT it leaves correctly and goes to the lobby, but the other clients are still seeing the user gameobject
  • Tobias
    Options
    Check the console output when this happens. Turn up the log level in PhotonNetwork if nothing else shows up. Make the Editor the one who leaves in one try, make it the one who stays in the room the other try.
    Did you turn off autoCleanup somewhere? Then instantiated stuff is staying in the room.
  • Zido
    Options
    I did, I get this error when I leave the room

    Cannot send messages when not connected; Either connect to Photon OR use offline mode!
    UnityEngine.Debug:LogError(Object)
    PhotonNetwork:VerifyCanUseNetwork() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1646)
    PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1446)
    PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/Extension/PhotonView.cs:165)
    Panda:FixedUpdate() (at Assets/Pandas/Programming/Scripts/Panda/Panda.cs:75)

    but this is because Panda.cs script is trying to send a RPC when the connection is off, I think it's not the problem. When other player leaves the room I don't get any errors.

    here is the Init.cs
    using UnityEngine;
    using System.Collections;
    public class Init : Photon.MonoBehaviour {
    
    	// Use this for initialization
    	void Start () {
    		PhotonNetwork.ConnectUsingSettings();
    		PhotonNetwork.player.name = "Jugador"+Random.Range(0,999999);
    		PhotonNetwork.autoCleanUpPlayerObjects = true;
    		PhotonNetwork.logLevel = PhotonLogLevel.Full;
    	}
    	
    	void OnJoinedRoom()
    	{
    		Debug.Log("Room: "+PhotonNetwork.room.name);
    		GameObject monster = PhotonNetwork.Instantiate("Panda/Panda",Vector3.zero,Quaternion.identity,0);
    		monster.name = "Player"+photonView.viewID.ID;
    	}
    	IEnumerator OnLeftRoom()
        {
            while(PhotonNetwork.room!=null || PhotonNetwork.connected==false)
                yield return 0;
            Application.LoadLevel(Application.loadedLevel);
        }
    	void OnPhotonRandomJoinFailed() 
    	{
    		PhotonNetwork.CreateRoom("1");
    		PhotonNetwork.JoinRoom("1");
    	}
    	void OnPhotonJoinRoomFailed()
    	{
    		PhotonNetwork.JoinRandomRoom();
    	}
    	void OnGUI()
        {
            if (PhotonNetwork.room == null)
    		{
    	        if (GUILayout.Button("Join random room"))
    	        {
    	            PhotonNetwork.JoinRandomRoom();
    	        }
    		}
    		else
    		{
    	        if (GUILayout.Button("Leave& QUIT"))
    	        {
    	            PhotonNetwork.LeaveRoom();
    	        }
    		}
        }
    }
    
    
    Weird but still doesn't work
  • Tobias
    Options
    It means you can't send messages when you're not connected. You obviously try to call some RPC after you called join or leave or something.
    The operation you tried to send was not sent at all, that's why nothing happens on the server.

    Make sure you are connected before you call operations or check the return-value of the operations you call (that will be false if it wasn't possible to execute).
  • Zido
    Options
    Sorry for the persistence but I cannot still leave the room in one client and see how photon destroys the gameobject and updates automatically everything in the other client, is there a way to do this manually? tell me and I'll do cause I'm tired of trying to fix it.
  • Tobias
    Options
    Set PhotonNetwork.autoCleanUpPlayerObjects to false and the objects of leaving players stay in the room.
    The owner will be null and only the MasterClient can affect those. This mode is supposed for full control of the existing objects, so you might have to figure out how to use them in your context.
  • Zido
    Options
    finally i fixed it, I'll post my solution here because I don't know if it's a PUN bug, but I had to make my Init.cs inherit from normal monobehaviour and not from photon.monobehaviour, also I needed to do another script inherit from photon.monobehaviour to manage other things, now when I push "leave" button photon correctly removes all player prefabs and updates everything, I'm pretty happy with photon! regards
  • Tobias
    Options
    Is your project small? Could you send it to us?
    It could be interesting to look why deriving from MonoBehaviour (instead from PhotonMonoBehaviour) had this effect.
    Or can you tell us how you setup everything?
  • Zido
    Options
    Hi Tobias, I think the problem was my own code and not PUN.
    I have some things separated now in different scripts, in the script where i have the "Leave" button I made it now inherit from photon.monobehaviour and is working either. Anyway you have the code of my original script in this thread so I think you could easily identify my error. Regards and sorry for long topic :D
  • r_projects
    edited October 2015
    Options
    I was in need to "OnPhotonPlayerDisconnected"

    Now I know, read the pdf ;p

    Hi just reading through this, could someone explain this code please ?

    I can’t seem to get the OnLeftRoom() to work, just need simple notification.

    I need something that all the other clients can detect that a player has left the room, I thought this was the case but never receive any notification.

    Ok I think I know what’s going on, I’m using this as detection of a disconnection (when client is closed [x]). But it never gets run as the client has gone.

    So how to detect a player has left room for all other clients to be notified?


    ..
    Enumerator OnLeftRoom()
        {
            while(PhotonNetwork.room!=null || PhotonNetwork.connected==false)
                yield return 0;
            Application.LoadLevel(Application.loadedLevel);
        }