Search Unity

Chat Stuff

Discussion in 'Multiplayer' started by nickavv, Mar 11, 2008.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I'm trying to write a chat program where the first person to open the file creates the 'game', and subsequent openers of the file join it. For some reason, each person just seems to have their own individual chat room to talk to themselves. What am I doing wrong here?

    Code (csharp):
    1. var chatLog = "";
    2. var userName = "Please Enter Your Name";
    3. var myText = "";
    4. var registered = false;
    5. var windowRect = Rect (5, 5, 350, 350);
    6.  
    7. function OnGUI () {
    8.     if (registered) {
    9.         windowRect = GUI.Window (0, windowRect, PlayerWindow, "Colorful Planet Chat");
    10.     } else {
    11.         userName = GUILayout.TextField (userName);
    12.         if (GUILayout.Button("Join the Chat!")) {
    13.             MasterServer.ClearHostList();
    14.             MasterServer.RequestHostList("WanillaCitrus001-chat");
    15.             if (MasterServer.PollHostList().length == 0) {
    16.                 Network.useNat = !Network.HavePublicAddress();
    17.                 Network.InitializeServer(5000, 25002);
    18.                 MasterServer.RegisterHost("WanillaCitrus001-chat","cpChat","");
    19.                 networkView.RPC ("announcePlayer", RPCMode.All);
    20.                 registered = true;
    21.             } else {
    22.                 var data : HostData[] = MasterServer.PollHostList();
    23.                 for (var element in data) {
    24.                     if (element.gameName == "cpChat") {
    25.                         if (Network.useNat) {
    26.                             print("Using Nat punchthrough to connect to host");
    27.                         } else {
    28.                             print("Connecting directly to host");
    29.                             Network.Connect(element.ip, element.port);
    30.                         }
    31.                     }
    32.                 }
    33.                 networkView.RPC ("announcePlayer", RPCMode.All);
    34.                 registered = true;
    35.             }  
    36.         }
    37.     }
    38. }
    39.  
    40. @RPC
    41. function sendMessage () {
    42.     if (myText != "") {
    43.         chatLog += "\n" + userName + ": " + myText;
    44.     }
    45. }
    46.  
    47. @RPC
    48. function announcePlayer () {
    49.     chatLog += "\n" + userName + " joined the chat!";
    50. }
    51.  
    52. function PlayerWindow (windowID : int) {
    53.     GUI.Label (Rect(5,5,250,250), chatLog);
    54.     myText = GUI.TextField (Rect(5, 250, 200, 20), myText);
    55.     if (GUI.Button(Rect(210, 250, 45, 20), "Send")) {
    56.         networkView.RPC ("sendMessage", RPCMode.All);
    57.         myText = "";
    58.     }
    59. }
     
  2. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Can anyone help me? I would use the Networking Example, only I want this to be more of a lobby rather than have only the users of the current game chatting.
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    I am sorry I don't have time to go through your code heavily, but here are a couple of tips;

    1. MasterServer.RequestHostList doesn't return instantly, so you don't want to call it, then try to poll the list in the same event code (button click) as it will only try to poll once.

    What I do, is call RequestHostList on app start, then provide a button for the user to refresh the list, which then calls it again. Then in my GUI display loop (no events, always runs) I poll the list and see if it contains any data, then I show that data (if any).

    And actually, since you start a server automatically if none are found, that will be your problem. (just looked over the code again)

    Also, for storing a chat list, it would actually be better to use an array (or specifically, an ArrayList) of strings, and simply add a new element to everyone's array when needed. This way you can prune really old messages and keep it clean, and you can also do per-line coloring or whatever else easier.

    HTH,
    -Jeremy