Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon InRoomChat script Switch Text

Discussion in 'Multiplayer' started by zotey, Sep 27, 2014.

  1. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Hello every one,
    I am working on a little prototype multiplayer game now and wanted to add a chat so i used the photon InRoomChat script, which comes whit the photon package. This scripts requires some changes to fit in my game but, i got stuck now i've been searching for hours...
    So i want to have the chat labels added on the bottom of my chat.
    I found on a ask topic that i needed to change line 52(the for loop) into:
    Code (CSharp):
    1.  for (int i = 0; i < messages.Count; i++)
    It do works but when a player plays the game and types something it get displayed, when another player joins the game and says something the label will get poped on top of the other label.
    I did try to change this by a foreach loop but the same problem.
    Can someone help me?

    Zotey

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(PhotonView))]
    6. public class InRoomChat : Photon.MonoBehaviour
    7. {
    8.     public Rect GuiRect = new Rect(0,0, 250,300);
    9.     public bool IsVisible = true;
    10.     public bool AlignBottom = false;
    11.     public List<string> messages = new List<string>();
    12.     private string inputLine = "";
    13.     private Vector2 scrollPos = Vector2.zero;
    14.  
    15.     public static readonly string ChatRPC = "Chat";
    16.  
    17.     public void Start()
    18.     {
    19.         if (this.AlignBottom)
    20.         {
    21.             this.GuiRect.y = Screen.height - this.GuiRect.height;
    22.         }
    23.     }
    24.  
    25.     public void OnGUI()
    26.     {
    27.         if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
    28.         {
    29.             return;
    30.         }
    31.        
    32.         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
    33.         {
    34.             if (!string.IsNullOrEmpty(this.inputLine))
    35.             {
    36.                 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
    37.                 this.inputLine = "";
    38.                 GUI.FocusControl("");
    39.                 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
    40.             }
    41.             else
    42.             {
    43.                 GUI.FocusControl("ChatInput");
    44.             }
    45.         }
    46.  
    47.         GUI.SetNextControlName("");
    48.         GUILayout.BeginArea(this.GuiRect);
    49.  
    50.         scrollPos = GUILayout.BeginScrollView(scrollPos);
    51.         GUILayout.FlexibleSpace();
    52.  
    53.         /* Defualt chat display system
    54.         for (int i = messages.Count - 1; i >= 0; i--)        // default script loop  this needed the change to for(int i =0; i < messages.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Count']Count[/URL]; i++)
    55.         {
    56.             GUILayout.Label(messages[i]);
    57.         }
    58.         */
    59.  
    60.         foreach(string message in messages)
    61.         {
    62.             GUILayout.Label(message);
    63.         }
    64.  
    65.         GUILayout.EndScrollView();
    66.  
    67.         GUILayout.BeginHorizontal();
    68.         GUI.SetNextControlName("ChatInput");
    69.         inputLine = GUILayout.TextField(inputLine);
    70.  
    71.         /*    send button
    72.         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
    73.         {
    74.             this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
    75.             this.inputLine = "";
    76.             GUI.FocusControl("");
    77.         }*/
    78.         GUILayout.EndHorizontal();
    79.         GUILayout.EndArea();
    80.     }
    81.  
    82.     [RPC]
    83.     public void Chat(string newLine, PhotonMessageInfo mi)
    84.     {
    85.         string senderName = "anonymous";
    86.  
    87.         if (mi != null && mi.sender != null)
    88.         {
    89.             if (!string.IsNullOrEmpty(mi.sender.name))
    90.             {
    91.                 senderName = mi.sender.name;
    92.             }
    93.             else
    94.             {
    95.                 senderName = "player " + mi.sender.ID;
    96.             }
    97.         }
    98.  
    99.         this.messages.Add("[" + senderName + "]" +": " + newLine);
    100.         if (messages.Count > 25)
    101.         {
    102.             messages.RemoveAt(0);
    103.         }
    104.         scrollPos.y = 100000;
    105.     }
    106.  
    107.     public void AddLine(string newLine)
    108.     {
    109.         this.messages.Add(newLine);
    110.     }
    111. }
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    You want to invert the order in which the messages get shown? If you want that, it looks ok.
    Or the order is OK but you want the messages to show up somewhere else?

    I don't understand this part: "the label will get poped on top of the other label" and I don't get the problem really well.
     
  3. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Hey
    Ty tobiass for the reply and yes I want to invert the order in which the messages get shown, but i when i use some of the code that's not the default one, let's say it is the same if you program two labels on the same position but whit another text.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    It's important that all the chat lines are in the scroll area. Maybe that area overlaps with some other Label(). It could be one from another script most likely.
    You should try to find out which .Label() call is causing the overlay. Find all of those calls in your project and add a debug string to identify each, if you must. Then move the overlapping label.
     
  5. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Well, i did this but the only .Label() that I used here is the label from the chat or better the labels in the for loop.
    Let me try to explain myself better, so a player joins my game and he is alone in the scene(no other players are joined, and he is connected to photon). When he send a chat message it is placed in his window like normal, so he will see this:
    1.png

    When another player joins the game an says something the first player will see this: 2.png And it when this player 2 says something else: 3.png Then it slides up but the first label stays in the same position.
    I hope i made myself clear now. I am not so good in english ;)

    Thanks for all help for now,

    Zotey
     
  6. GSZ

    GSZ

    Joined:
    Dec 13, 2012
    Posts:
    11
    I'm actually having the exact same problem with this script.

    I'll try to describe the problem a bit more in depth...

    When two players are in the room together and both use the chat box, they both share the chat box, but the messages seem to write on top of each other.

    For example - player 1 writes two messages producing two lines of chat.

    Now if player 2 writes a message, instead of the message showing up on line 3 (the desired result), his messages appears on line 1 (right on top of player 1's first message). If player 2 writes a second message, it appears on line 2, again right on top of player 1's second message.

    If anybody is able to determine what is causing this, please do let us know.

    Thanks!
     
    zotey likes this.
  7. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    hi,

    had the same problem..
    What i've done was changing this: upload_2014-10-1_16-16-50.png
    changing the messages size. if that doesn't work, change the hight of your GUI rect to 600

    Rens
     
  8. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    T
    That may work, but i found the problem you need to make sure that you have only 1 InRoomChat script enabled in your scene that is causing the two labels pop in the same position. I could fix this by disabling one of the scripts(i needed to make sure i did enable it the local player and disable it everywhere else) but then i got the problem that my player is retrieving the messages(found out whit debug.log), but that they are not displayer as they are added to another list i think (use debug.log(messages.count)).
    What now?;)
     
    GSZ likes this.
  9. calvintmoss

    calvintmoss

    Joined:
    Jul 4, 2014
    Posts:
    8
    Unrelated to the question but @zotey, is that all the code you used for the chat function I am still learning photon and I was hoping to implement chat. If not do you have any tutorials for setting up chat? Thanks!
     
  10. GSZ

    GSZ

    Joined:
    Dec 13, 2012
    Posts:
    11
    We seem to be working through the exact same issues. I'm having this problem now as well. I'm pretty sure you need to add an OnPhotonSerializeView function into the script, I just don't quite know how to add it successfully yet...

    I've added the following code to the end of the script:
    Code (CSharp):
    1.  
    2. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    3.      
    4.         if (stream.isWriting) {
    5.             //This is OUR player. We need to send our messages to the network
    6.             stream.SendNext(inputLine);
    7.  
    8.         }
    9.         else {
    10.             //This is someone else's player. We need to receive their messages.
    11.             inputLine = (string)stream.ReceiveNext();
    12.  
    13.         }
    14.      
    15.     }
    16.  
    but it doesn't seem to really be doing anything at all... anybody have any ideas?
     
    Last edited: Oct 2, 2014
  11. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Well, for you this is a utility script from photon so if you have photon installed you could use it.
    I don't think you need to do this as you are sending your message in a RPC. I have tested that the text that the player sends, is send to all players if you add a debug.log(newline); in your rpc in this case at line 96.
    When you build it and test it with the a client and unity running you will see that if the client sends a message this message is printed in the console.
     
  12. GSZ

    GSZ

    Joined:
    Dec 13, 2012
    Posts:
    11
    The problem I'm having is that I don't know what to attach the script to. If I attach it to my playercontroller, it bugs out in
    in the way Zoti posted in his screen shot below:


    Basically, each client's messages just write on top of each other. If I attach the script to an empty game object in the scene, the messages don't show up on non-local clients.
     
  13. GSZ

    GSZ

    Joined:
    Dec 13, 2012
    Posts:
    11
    Ok - I've figured it out.

    Zotey, instead of attaching the script to a player object, create an empty object (I named it Chat) in your scene with a Photon View and attach the script to that.

    Works like a charm for me now.

    And one more thing - If you're having problems with the way your scrollbox is scrolling (scrolling up instead of down), insert this line of code into your RPC:

    scrollPos.y = Mathf.Infinity;

    Your scrollview will now follow your chat down by default and allow the player to scroll up to view older messages.
     
    zotey likes this.
  14. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    This is indeed the solution thank you GSZ.:)