Friends List using Local PlayerPrefs Database and FindFriends() Not Working

Options
Besides the many minute flaws with this code, the overall Friends List doesn't seem to work at all. You can add friends, but they will always show up as Offline, even if the users are in the lobby or the same room. I don't know if it's an issue on my end or Photon's, because (as far as I know), this should work perfectly fine.

Here's the code:


using UnityEngine;
using System.Collections;

public class friendList : MonoBehaviour {

string[] friends;

public char[] splitVar;

public string friendToAdd = "Enter Friend Name...";

Vector2 scroll = Vector2.zero;

string playerName;

void OnJoinedLobby()
{
updateFriends();

playerName = "Player " + Random.Range(0, 999);
PhotonNetwork.playerName = playerName;
}

public void updateFriends()
{
friends = PlayerPrefs.GetString("friendList").Split(splitVar, 200);
Debug.Log(PlayerPrefs.GetString("friendList"));
PhotonNetwork.FindFriends(friends);
}

public void removeFriend(string name)
{
PlayerPrefs.SetString("friendsList", "");
foreach(string pl in friends)
{
if(pl != name)
{
PlayerPrefs.SetString("friendList", PlayerPrefs.GetString("friendList") + name + "/");
}
}
updateFriends();
}


void OnGUI()
{
if (PhotonNetwork.connectedAndReady)
{
GUILayout.BeginArea(new Rect(10, 10, 1000, 600));
GUILayout.BeginHorizontal();
GUILayout.BeginVertical("Box", GUILayout.Width(500));
GUILayout.Label(PhotonNetwork.playerName);
GUILayout.Label("Friends: ");


scroll = GUILayout.BeginScrollView(scroll, "Window");

if (PhotonNetwork.Friends != null)
{
foreach (FriendInfo friend in PhotonNetwork.Friends)
{
if (friend.Name != "")
{
GUILayout.BeginHorizontal("Box");
GUILayout.Label(friend.Name);
if (friend.IsOnline)
{
if (friend.IsInRoom)
{
if (GUILayout.Button("Join"))
{
PhotonNetwork.JoinRoom(friend.Room);
}
}
else
{
GUILayout.Label(" [In Lobby]");
}
}
else
{
GUILayout.Label(" [Offline]");
}
if (GUILayout.Button("X", GUILayout.Width(50))) ;
{
removeFriend(friend.Name);
}
GUILayout.EndHorizontal();
}
}
} else
{
GUILayout.Label("No Friends Found... ");
}

GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
friendToAdd = GUILayout.TextField(friendToAdd);
if (GUILayout.Button("Add", GUILayout.Width(100)))
{
PlayerPrefs.SetString("friendList", PlayerPrefs.GetString("friendList") + friendToAdd + "/");
updateFriends();
}
if (GUILayout.Button("R", GUILayout.Width(50)))
{
PhotonNetwork.Disconnect();
Application.LoadLevel(0);
}
if (GUILayout.Button("X", GUILayout.Width(50)))
{
PlayerPrefs.SetString("friendList", "");
updateFriends();
}
GUILayout.EndHorizontal();


GUILayout.EndVertical();
GUILayout.BeginVertical("Box", GUILayout.Width(500));
int count = PhotonNetwork.countOfPlayersInRooms + PhotonNetwork.countOfPlayersOnMaster;
GUILayout.Label("Players Online: (" + count + ")");

foreach(PhotonPlayer pl in PhotonNetwork.playerList)
{
GUILayout.Label(pl.name);
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}



}

Comments

  • vadim
    Options
    Friend list works only while client is joined to lobby. Make sure that you are not in room.