Okay, so I finally just got the chance to start trying to toy around with the friends list again. I’m trying to use FindFriends while connected to a Room… Here’s what I have so far:
I build my friends list from a string in PlayerPrefs. I know this is working.
After that, once the player joins the room, there i have these lines in the Update() function of my NetworkCharacter script, which is attached to my player:
if(!gotFriends)
{
string[] friends = FriendsList.ToArray();
PhotonNetwork.FindFriends(friends);
if (PhotonNetwork.Friends != null)
{
Debug.Log(PhotonNetwork.Friends.Count);
foreach (FriendInfo info in PhotonNetwork.Friends)
{
string s = string.Empty;
if (info.IsInRoom)
s = info.Room.ToString();
mFriendsList.Add(new FriendsInfo(info.Name.ToString(), s, info.IsOnline));
}
gotFriends = true;
}
}
This SHOULD populate mFriendsList, right?
Well, it is throwing 2 errors when I try and do this…
You don’t mention where that “if” statement resides, it belongs inside a Photon callback like OnPhotonPlayerConnected or something, that way it won’t try and pull in the friends info until you are actually connected to a Photon server.
Sorry - re-read your post, you should remove this from Update () and only call it once
Ahh, that makes sense. The gotFriends bool is set to true at the beginning, and then OnJoinedLobby i set it to false so that it can build the friends list.
If I only call it once, how will I be able to update friends statuses? Is there an enum I’m missing somewhere? Like OnUpdatedFriends or something?
AHH, they do have an OnFriendsListUpdated() function… But it still isn’t working… It isn’t calling at all… Does FindFriends only call back if there is a friend online?
Okay, one last question… How do I get updates from players? Like, if a person is offline and then comes online while my player is in a room, how can I update that friends status?
I’ll have to check the other callbacks and see what’s available.
In thinking about this, I’d much rather have clients post messages instead of constantly polling for data. So when your new player joins the server it can pretty easily loop through all friends and send a “ is online” message.
Hmm… That could work for sure… I’d have to think about how this could work…
The only problem I could see there is like if I’m in a room and player 2 is in the lobby, how could one send that message? Maybe over Photon Chat… That seems like it could work if I ran them in parallel.
Ahh… Another thing I just thought about, at least with my friends system… If I added player 2 to my friends list, it doesn’t necessarily mean that I am on player 2’s friends list… Which means i wouldn’t be able to know who to send the message to.
It sounds like it would still work but you’d need a friend invite list and use the actual friends list for those that are confirmed or approved.
I’m working on integrating Photon with my Wordpress site and now I’m going to consider adding a friend function to it. Ideally you can add a friend from the site (forums) and in-game, that will be fun! Having to pay extra for Photon chat just rubs me the wrong way
The friends list is not updated automatically from the server. For that, the server just doesn’t know good enough how long a player actually uses it and if the client is still on that screen, etc.
So instead of sending updates, the clients have to poll the list. In best case, just once every minute and only while on the friends-list screen.
It’s not as comfy as we would like but we wanted to give more control to the client-side in this case.
I can’t poll my friends list while in a room, though… It always gives me an error. And the only time the friends list is accessible is when you are in a room, playing the game.
@Klaresw : Noted! I can’t promise anything at the moment though. There are a few things we need to tackle more urgently, sorry.
@Epictickle :
That issue is happening because you can’t find friends while in a room.
This is due to the way we separated servers. The room is on a “Game Server” which neither knows the room list for your title and neither the players in those rooms. You need to be on the Master Server (in no room), so you need to fetch the friends list while not playing in a room.
Sorry.
I have a work around for this problem…
I use a mysql database with time, username,onlinestatus,roomname
than i call this in php script:
<?php
$time = time();
$time_check = $time - 60;
//SET TIME 20 Minutes
$email = $_POST["email"];
//Sets the inbound user email
$room = $_POST["room"];
//Sets the inbound user roomname
$host = "localhost";
// Host name
$username = "";
// Mysql username
$password = "";
// Mysql password
$db_name = "";
// Database name
$tbl_name = "user_online";
// Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password") or die("cannot connect to server");
mysql_select_db("$db_name") or die("cannot select DB");
$mysqli = new mysqli($host, $username, $password, $db_name);
$sql = "SELECT * FROM $tbl_name WHERE UserName='$email'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == "0") {
$sql1 = "INSERT INTO $tbl_name(UserName, time, roomname, onlinestatus)VALUES('$email', '$time', '$room', '1')";
$result1 = mysql_query($sql1);
} else {
$sql2 = "UPDATE $tbl_name SET time='$time', roomname='$room', onlinestatus ='1' WHERE UserName = '$email'";
$result2 = mysql_query($sql2);
}
$sql3 = "SELECT * FROM $db_name.user_online;";
$result3 = $mysqli->query($sql3);
while ($usersonline = $result3->fetch_array(MYSQLI_ASSOC)){
$users .= $usersonline["UserName"] . "," . $usersonline["roomname"] . "|";
}
//$count_user_online = mysql_num_rows($result3);
echo $users;
// if over 20 minute, delete session
$sql4 = "DELETE FROM $tbl_name WHERE time<$time_check";
$result4 = mysql_query($sql4);
/* free result set */
$result3->free();
/* close connection */
$mysqli->close();
mysql_close();
?>
this script will check if any of the clients have not sent a “ping for lack of better words” in 60 seconds they are deleted from the online list and no longer online
I use a www form to send the email and roomname to this script every 30 seconds
you than read the response in your c# or JS script to populate a friends list that will update every “in my case” 30 seconds checking to see who all is online.
you can than compare the results to a stored list of friends to see A) if they are online B) what room C) join them by exiting to lobby and joining them in the new room by name.