PhotonNetwork.GetRoomList() gone in PUN2. How can I modify this to work again?

Pretty Much what the title says. I've already spent hours trying. I'm still new to photon and followed a tutorial a while ago to get this working. I'm not sure how to cache the room lists. Any help would be appreciated.

namespace UnityEngine.UI.Extensions
{
using System.Collections.Generic;
using UnityEngine;

public class RoomLayoutGroup : MonoBehaviour
{

[SerializeField]
private GameObject _roomListingPrefab;
private GameObject RoomListingPrefab
{
get { return _roomListingPrefab; }
}

private List _roomListingButtons = new List();
private List RoomListingButtons
{
get { return _roomListingButtons; }
}


private void OnReceivedRoomListUpdate()
{
RoomInfo[] rooms = PhotonNetwork.GetRoomList();

foreach (RoomInfo room in rooms)
{
RoomReceived(room);
}

RemoveOldRooms();
}

private void RoomReceived(RoomInfo room)
{
int index = RoomListingButtons.FindIndex(x => x.RoomName == room.Name);

if (index == -1)
{
if (room.IsVisible && room.PlayerCount < room.MaxPlayers)
{
GameObject roomListingObj = Instantiate(RoomListingPrefab);
roomListingObj.transform.SetParent(transform, false);

RoomListing roomListing = roomListingObj.GetComponent();
RoomListingButtons.Add(roomListing);

index = (RoomListingButtons.Count - 1);
}
}

if (index != -1)
{
RoomListing roomListing = RoomListingButtons[index];
roomListing.SetRoomNameText(room.Name);
roomListing.Updated = true;
}
}

private void RemoveOldRooms()
{
List removeRooms = new List();

foreach (RoomListing roomListing in RoomListingButtons)
{
if (!roomListing.Updated)
removeRooms.Add(roomListing);
else
roomListing.Updated = false;
}

foreach (RoomListing roomListing in removeRooms)
{
GameObject roomListingObj = roomListing.gameObject;
RoomListingButtons.Remove(roomListing);
Destroy(roomListingObj);
}
}

}
}

Comments

  • Hi @Arke12917,

    you can take a look at the Asteroids demo from the PUN 2 package and especially its LobbyMainPanel class. This one contains all the logic related to caching a room list and deal with the OnRoomListUpdate callback.
  • Arke12917Arke12917
    edited October 9
    @Christian_Simon Thanks for your response but I already checked that out. There's a lot of information in there and I couldn't quite decipher how to integrate it with this script. I only posted here cause I was getting pretty desperate for answers. There's barely any new info on PUN 2 outside of these forums. I can't revert back to classic PUN because of a bug that causes random client disconnects when executing large RPC functions. I've tried, I genuinely have but I'm not sure why it doesn't work. It was much simpler back in PUN classic. Here it is with the changes I tried:
    namespace UnityEngine.UI.Extensions
    {
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;
    using Photon.Realtime;
    using ExitGames.Client.Photon;

    public class RoomLayoutGroup : MonoBehaviour
    {
    [SerializeField]
    private GameObject _roomListingPrefab;
    private GameObject RoomListingPrefab
    {
    get { return _roomListingPrefab; }
    }
    public static RoomInfo[] rooms;

    private Dictionary cachedRoomList;
    private List _roomListingButtons = new List();
    public List RoomListingButtons
    {
    get { return _roomListingButtons; }
    }





    public void OnRoomListUpdate(List RoomlistingButtons)
    {
    print("updating rooms");
    foreach (RoomInfo info in RoomlistingButtons)
    {
    // 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;
    }
    // Add new room info to cache
    else
    {
    cachedRoomList.Add(info.Name, info);
    }
    }
    }


    private void OnReceivedRoomListUpdate()
    {
    rooms = new RoomInfo[cachedRoomList.Count];
    cachedRoomList.Values.CopyTo(rooms, 0);

    foreach (RoomInfo room in rooms)
    {
    RoomReceived(room);
    }

    RemoveOldRooms();
    }

    private void RoomReceived(RoomInfo room)
    {
    int index = RoomListingButtons.FindIndex(x => x.RoomName == room.Name);

    if (index == -1)
    {
    if (room.IsVisible && room.PlayerCount < room.MaxPlayers)
    {
    GameObject roomListingObj = Instantiate(RoomListingPrefab);
    roomListingObj.transform.SetParent(transform, false);

    RoomListing roomListing = roomListingObj.GetComponent();
    RoomListingButtons.Add(roomListing);

    index = (RoomListingButtons.Count - 1);
    }
    }

    if (index != -1)
    {
    RoomListing roomListing = RoomListingButtons[index];
    roomListing.SetRoomNameText(room.Name);
    roomListing.Updated = true;
    }
    }

    private void RemoveOldRooms()
    {
    List removeRooms = new List();

    foreach (RoomListing roomListing in RoomListingButtons)
    {
    if (!roomListing.Updated)
    removeRooms.Add(roomListing);
    else
    roomListing.Updated = false;
    }

    foreach (RoomListing roomListing in removeRooms)
    {
    GameObject roomListingObj = roomListing.gameObject;
    RoomListingButtons.Remove(roomListing);
    Destroy(roomListingObj);
    }
    }

    }
    }
  • In PUN 2 your class either has to be derived from MonoBehaviourPunCallbacks or has to implement the ILobbyCallbacks interface to make OnRoomListUpdate work. Since it is easier to derive your class from MonoBehaviourPunCallbacks (this way you can simply override the callbacks you want to use), I'm asking you to do this.

    Then you can add the public override void OnRoomListUpdate(List<RoomInfo> roomList) callback. Whenever this gets called, the client receives a list containing only new and updated rooms, it isn't the entire room list. In the LobbyMainPanel class from the Asteroids demo you can take a look at the UpdateCachedRoomList function. This one gets called from the OnRoomListUpdate callback and updates the cached room list properly.
Sign In or Register to comment.