Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Third Party [PHOTON] How to handle a friends list?

Discussion in 'Multiplayer' started by Epictickle, Dec 4, 2014.

  1. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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:


    Code (CSharp):
    1. if(!gotFriends)
    2.             {
    3.                 string[] friends = FriendsList.ToArray();
    4.                 PhotonNetwork.FindFriends(friends);
    5.  
    6.                 if (PhotonNetwork.Friends != null)
    7.                 {
    8.                     Debug.Log(PhotonNetwork.Friends.Count);
    9.                     foreach (FriendInfo info in PhotonNetwork.Friends)
    10.                     {
    11.                         string s = string.Empty;
    12.  
    13.                         if (info.IsInRoom)
    14.                             s = info.Room.ToString();
    15.  
    16.                         mFriendsList.Add(new FriendsInfo(info.Name.ToString(), s, info.IsOnline));
    17.                     }
    18.  
    19.                     gotFriends = true;
    20.                 }
    21.  
    22.              
    23.             }

    This SHOULD populate mFriendsList, right?

    Well, it is throwing 2 errors when I try and do this...

    Error 1:
    Error 2:
    What the heck is going on here?
     
  2. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    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
     
  3. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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?
     
  4. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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?

    here's my code:

    Code (CSharp):
    1. public void OnJoinedLobby()
    2.     {
    3.         //PhotonNetwork.FindFriends(FriendsList.ToArray());
    4.         string[] friends = FriendsList.ToArray();
    5.         PhotonNetwork.FindFriends(friends);
    6.  
    7.         //StartCoroutine(WaitForFriends());
    8.     }
    9.  
    10.     public void OnUpdatedFriendsList()
    11.     {
    12.         Debug.Log("Updated: " + PhotonNetwork.Friends.Count.ToString());
    13.     }
    Just put a debug in there to see if it was calling... Definitely isn't lol.

    Update: I fixed it lol. I was using OnUpdatedFriendsList()... It Friends needed to be singular. xP
     
    Last edited: Dec 5, 2014
  5. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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?
     
  6. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    Hmmm, that's one I have no idea, :-(

    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 "<username> is online" message.

    What do you think?
     
  7. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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.
     
    Last edited: Dec 6, 2014
  8. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    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 :(
     
  9. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    That's very true. Its a good alternative in games since people can all be in the chat server but be connected to different rooms on PUN.

    Yeah that seems like a good way to do it. I appreciate it man!
     
  10. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,083
    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.
     
  11. Klaresw

    Klaresw

    Joined:
    Dec 9, 2014
    Posts:
    2
    I would like to more comfortably))
     
  12. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    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.
     
  13. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,083
    @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.
     
  14. zer0c00l42066

    zer0c00l42066

    Joined:
    Jun 9, 2013
    Posts:
    2
    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:


    Code (PHP):
    1. <?php
    2. $time = time();
    3. $time_check = $time - 60;
    4. //SET TIME 20 Minutes
    5. $email = $_POST["email"];
    6. //Sets the inbound user email
    7. $room = $_POST["room"];
    8. //Sets the inbound user roomname
    9. $host = "localhost";
    10. // Host name
    11. $username = "";
    12. // Mysql username
    13. $password = "";
    14. // Mysql password
    15. $db_name = "";
    16. // Database name
    17. $tbl_name = "user_online";
    18. // Table name
    19.  
    20. // Connect to server and select databse
    21. mysql_connect("$host", "$username", "$password") or die("cannot connect to server");
    22. mysql_select_db("$db_name") or die("cannot select DB");
    23. $mysqli = new mysqli($host, $username, $password, $db_name);
    24. $sql = "SELECT * FROM $tbl_name WHERE UserName='$email'";
    25. $result = mysql_query($sql);
    26.  
    27. $count = mysql_num_rows($result);
    28.  
    29. if ($count == "0") {
    30.  
    31.     $sql1 = "INSERT INTO $tbl_name(UserName, time, roomname, onlinestatus)VALUES('$email', '$time', '$room', '1')";
    32.     $result1 = mysql_query($sql1);
    33. } else {
    34.     $sql2 = "UPDATE $tbl_name SET time='$time', roomname='$room', onlinestatus ='1' WHERE UserName = '$email'";
    35.     $result2 = mysql_query($sql2);
    36. }
    37.  
    38. $sql3 = "SELECT * FROM $db_name.user_online;";
    39. $result3 = $mysqli->query($sql3);
    40.  
    41. while ($usersonline = $result3->fetch_array(MYSQLI_ASSOC)){
    42.  
    43.     $users .= $usersonline["UserName"] . "," . $usersonline["roomname"] . "|";
    44. }
    45. //$count_user_online = mysql_num_rows($result3);
    46.  
    47. echo $users;
    48.  
    49. // if over 20 minute, delete session
    50. $sql4 = "DELETE FROM $tbl_name WHERE time<$time_check";
    51. $result4 = mysql_query($sql4);
    52.  
    53. /* free result set */
    54. $result3->free();
    55.  
    56. /* close connection */
    57. $mysqli->close();
    58. mysql_close();
    59. ?>
    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

    the return looks like this:

    misty@whatever.com,test|tom@whatever.com,test|


    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.

    Cheers and good luck
     
    ponghood likes this.