[common programming question] Custom array returns nothing
Hello dear community!
I’ve been trying to solve a problem of getting a list of active servers (rooms) and displaying them to the player. I am using Photon Unity free extension. Their code library and API are quite detailed, I tried to follow strictly, but I still don’t know why it works incorrectly. Could you have a look?
Their API says to use “static RoomInfo [] PhotonNetwork.GetRoomList()”, which I do. And it gets called correctly. But the value it returns– is nothing. It doesn’t say anything. Not “null”, not “0” , - nothing. It should return an array of items of RoomInfo type, as I understood. Maybe I should declare a list or an array to use it even if it is of common type?
The console log: "RoomInfo[]"
Here’s the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class MainMenu2 : MonoBehaviour {
void Update () {
if (Input.GetKeyDown (KeyCode.B)) {
Debug.Log (PhotonNetwork.GetRoomList ());
}
}
public void Connect() {
Debug.Log("connect()");
PhotonNetwork.ConnectUsingSettings( "New Unity Project" );
}
public void OnJoinedLobby() {
Debug.Log ("OnJoinedLobby");
PhotonNetwork.JoinRandomRoom();
}
public void OnPhotonRandomJoinFailed() { //when no room can be entered (all rooms are full or no rooms at all)
Debug.Log ("OnPhotonRandomJoinFailed");
PhotonNetwork.CreateRoom( null ); //null = creates a room with a random name
}
}
(If you are interested, the API is located here, search for “GetRoomList” : http://doc-api.photonengine.com/en/pun/current/class_photon_network.html#aeef2085375accb7d4bc88e60cbe15eb9)
Thank you very much!
Answer by Landern · Oct 28, 2015 at 01:07 PM
You probably need to refactor your code, but lets say you use the key 'b' to refresh the room list. Based off the console log you are in fact getting an array of RoomInfo types back, now you need to hold on to that value(s) and use each instance of RoomInfo in the array to pull out the information you want to display for the user to select a preexisting room to join.
Below is an example of storing the array of RoomInfo types in a local private variable and using debug.log to display some information. This is probably something you would want to put in your GUI in a list for selection if any rooms exist.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class MainMenu2 : MonoBehaviour {
private RoomInfo[] availableRooms = null;
void Update () {
if (Input.GetKeyDown (KeyCode.B)) {
availableRooms = PhotonNetwork.GetRoomList();
if (availableRooms != null && availableRooms.Length > 0){
Debug.Log("Number of available rooms: " + availableRooms.Length); // Length will get you the number of items in the array
for (int i = 0; i < availableRooms.Length; i++)
Debug.Log("Available Room #" + i+1 + " - Room name: " + availableRooms.name);
} else {
Debug.Log("There are preexisting rooms available");
}
}
}
public void Connect() {
Debug.Log("connect()");
PhotonNetwork.ConnectUsingSettings( "New Unity Project" );
}
public void OnJoinedLobby() {
Debug.Log ("OnJoinedLobby");
PhotonNetwork.JoinRandomRoom();
}
//when no room can be entered (all rooms are full or no rooms at all)
public void OnPhotonRandomJoinFailed() {
Debug.Log ("OnPhotonRandomJoinFailed");
PhotonNetwork.CreateRoom( null ); //null = creates a room with a random name
}
}
Answer by Fewpwew130 · Oct 28, 2015 at 09:11 PM
Landern, thank you very much!!!!!!!!!!!
You quickly replied to my question, you understood it, you answered it, you provided a working code and fully commented it. WOW. Thank you. Networking is hard for me and you helped a lot.
Your answer
Follow this Question
Related Questions
changing reference types to value types 1 Answer
Remove connection between lists or arrays 3 Answers
List.contain only one variable 1 Answer
I have trouble understanding arrays and enums. When and how? 0 Answers
All GameObjects list to a GameObject? 0 Answers