PUN 2 Problem

Options

I have a problem here with Photon's PUN 2. Sometimes It works, but other times it doesn't. Since the last 2 weeks it isn't working that fine. Before it were better, I joined to the master, and then, to the lobby and It allowed me to list the rooms and join them without any problem. Now, I haven't changed that code that much, I only changed it after the errors started. Now, sometimes it joins a match, but another it doesn't, showing the following error:
JoinRandomRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.

I only have 2 devices to test my online with PUN, but, even if I created a room is not working anymore, it just seems like works randomly. Here's my code if you want to check it:
void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster()
{
    //This shows a popup to let know the player that is connected 
    //PhotonNetwork.JoinLobby();
    base.OnJoinedLobby();
    GameObject.Find("IWifi").SetActive(false);
    print("Puga");
    StatePScript.IsShow = true;
    Connected = true;
    GameObject.Find("IOk").GetComponent<Image>().color = Color.white;
    print("IOKS Value is " + IOKS.Show);
    GameObject.Find("StatusText").GetComponent<Text>().text = "Connected!";
    GameObject.Find("StatusText").GetComponent<Text>().color = Color.green;
    A.Play();
    GameObject.Find("StatePanel").GetComponent<Animator>().SetBool("Show", false);
    IOKS.Show = false;
    GameObject.Find("IOk").GetComponent<Image>().color = new Color(0, 0, 0, 0);
    Connected = false;
    StatePScript.IsShow = false;
}
I have other photon scripts like this that is the random room code:
using Photon.Pun;
public class RandomBScript : MonoBehaviourPunCallbacks
{
    private Button B;
    void Start ()
    {
        B = GetComponent<Button>();
        B.onClick.AddListener(Clicker);
    }
    void Clicker()
    {
        PhotonNetwork.JoinRandomRoom();
        print("Random");
    }
    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        base.OnJoinRandomFailed(returnCode, message);
        print(message);
    }
The create room code:
using Photon.Pun;
public class CreateRoomS : MonoBehaviourPunCallbacks
{
 private Button B
 private InputField IF;
 private InputField PlayerField;
public AudioSource A;
void Start ()
{
    B = GetComponent<Button>();
    PlayerField = GameObject.Find("PlayerInput").GetComponent<InputField>();
    IF = GameObject.Find("NameInput").GetComponent<InputField>();
    B.onClick.AddListener(Clicker);
}
void Clicker()
{
    print("Trying To create a room...");
    if (IF.text.Length > 0 && IF.text.Length <= 20)
    {
        int PlayerAmount = Int32.Parse(PlayerField.text);
        RoomOptions roompos = new RoomOptions()
         {
            IsVisible = true, IsOpen = true, MaxPlayers = (byte)PlayerAmount
         };
        PhotonNetwork.CreateRoom(IF.text, roompos);
        print("RoomCreated!");
    }
    else
    {
        A.Play();
    }
}
public override void OnJoinedRoom()
{
    print("We are in a room");
    PhotonNetwork.LoadLevel("WaitScene");
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
    base.OnCreateRoomFailed(returnCode, message);
}
public override void OnCreatedRoom()
{
    base.OnCreatedRoom();
    print("We created the room");
}
public override void OnConnectedToMaster()
{
    base.OnConnectedToMaster();
    print("Connected to the master");
}
And finally, the list rooms code:
using Photon.Pun;
using System;
public class RooManager : MonoBehaviourPunCallbacks
{
    public GameObject roomPrefab;
    public Sprite Four, Two, Three;
    private string RoomName;
    private int PlayerAmount;
    private int MaxPlayers;
    private Image I;
    private Vector2 RoomVector;
    private bool Lock = false;
    public GameObject Content;
    private List<RoomInfo> RoomList;
    private bool IsntNull = false;
    private Dictionary<string, RoomInfo> cachedRoomList;
    private Dictionary<string, GameObject> roomListEntries;
    private void Awake()
    {
        GameObject.Find("StatePanel").GetComponent<Animator>().SetBool("Show", true);
        cachedRoomList = new Dictionary<string, RoomInfo>();
        roomListEntries = new Dictionary<string, GameObject>();
    }
    void Start ()
    {
        Content = GameObject.Find("Content").GetComponent<GameObject>();
        RoomVector = new Vector2(370, this.transform.position.y);      
    }
    private void ClearRoomListView()
    {
        foreach (GameObject entry in roomListEntries.Values)
        {
            Destroy(entry.gameObject);
        }
        roomListEntries.Clear();
    }

    private void UpdateRoomListView()
    {
        foreach (RoomInfo Item in cachedRoomList.Values)
        {
            RoomName = Item.Name;
            PlayerAmount = Item.PlayerCount;
            MaxPlayers = Item.MaxPlayers;
            RoomVector.y -= 100;
            GameObject RoomPrefab = Instantiate(roomPrefab, RoomVector, transform.rotation) as GameObject;
            if (Item.PlayerCount == 0)
            {
                Destroy(RoomPrefab);
            }
            print(PhotonNetwork.CurrentLobby.Name);
            RoomPrefab.transform.Find("RoomName").GetComponent<Text>().text = RoomName;
            RoomPrefab.transform.Find("PlayerInt").GetComponent<Text>().text = PlayerAmount.ToString();
            if (Item.MaxPlayers == 4)
            {
                GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Four;
            }
            else if (Item.MaxPlayers == 2)
            {
                GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Two;
            }
            else if (Item.MaxPlayers == 3)
            {
                GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Three;
            }
            RoomPrefab.transform.SetParent(Content.transform);
        }
    }
    public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        ClearRoomListView();
        UpdateCachedRoomList(roomList);
        UpdateRoomListView();
        print("Updated");
    }
    private void UpdateCachedRoomList(List<RoomInfo> roomList)
    {
        foreach (RoomInfo info in roomList)
        {
            // Remove room from cached room list if it got closed, became invisible or was marked as removed
            if (!info.IsOpen || !info.IsVisible || info.RemovedFromList)
            {
                if (cachedRoomList.ContainsKey(info.Name))
                {
                    cachedRoomList.Remove(info.Name);
                }
                continue;
            }

            // Update cached room info
            if (cachedRoomList.ContainsKey(info.Name))
            {
                cachedRoomList[info.Name] = info;
            }
            else
            {
                cachedRoomList.Add(info.Name, info);
            }
        }
    }
This script instantiates a button for every room in photon's server. If you click one button, you join that room. As I said, sometimes it work and sometimes it doesn't, sometimes it helped to comment the line PhotonNetwork.JoinLobby(), but that means that you wont see the rooms. Actually, even with or without the JoinLobby() line, it isn't working that well.

Comments

  • Hi @SebGM2018,

    in your first code snippet you are calling base.OnJoinedLobby(); in the OnConnectedToMaster callback, These are two different operations and I'm not sure if it works this way. Instead please use PhotonNetwork.JoinLobby(); in the OnConnectedToMaster callback and wait for the OnJoinedLobby callback. This one tells you, that the client has successfully joined the lobby where you can list all available rooms.