x


Chat order

When chatting in my chat bar it stacks the inputs. I would like to put them into a queue rather.

Meaning that the items display:

0(new chat message) 1 2 3 4 5th chat message

Here's the stack code for (int i = messages.Count - 1; i >= 0; i--) { GUILayout.Label(messages[i]); }

I can't wrap the logic around my mind to get the items to display like this:

5th (chat message) 4 3 2 1 0 (new chat message)

more ▼

asked Aug 28, 2013 at 12:59 AM

KuPAfoo gravatar image

KuPAfoo
88 24 40 57

Here's what I use, if this doesn't match up with what you were asking I'll convert it to a comment:

(Globals)

 // PlayerLog
 private int _maxLines = 8;
 private Queue<string> _queue = new Queue<string>();
 private string Mytext = "";

(Methods)

 public void NewActivity(string activity) {
 if (_queue.Count >= _maxLines)
     _queue.Dequeue();
 
 _queue.Enqueue(activity);
 
 Mytext = "";
 foreach (string st in _queue)
     Mytext = Mytext + st + "\n";
 }

in OnGUI

 // display playerLog
     GUI.Label(new Rect(2,                             // x, left offset
             (Screen.height - 150),                    // y, bottom offset
             300f,                                    // width
             150f), Mytext,plogStyle);    // height, text, Skin features

and my other scripts pass messages to plog.NewActivity();

Aug 28, 2013 at 01:13 AM getyour411

I don't ever see Plog declared :

is this the monobehavior you reference as class.NewActivity();?

Aug 28, 2013 at 03:44 AM KuPAfoo

Yes, "Plog" is just what I declare in my other scripts like Plog = GameObject.Getcomponent().NewActivity("this is a new message")

Aug 28, 2013 at 03:49 AM getyour411

Oi, you are using Photon and networking stuff and honestly I don't know if my method plays nicely with that, so I'll change this to a comment/unanswered.

Aug 28, 2013 at 03:51 AM getyour411

okay, well thanks for your help! I think your on the right track...

I've narrowed it down to a few lines of code making this print: scrollPos = GUILayout.BeginScrollView(scrollPos);

         GUI.color = Color.white;
         for (int i = messages.Count - 1; i >= 0; i--)
         {
         //    if(i>1)
             //GUILayout.Label.Remove (messages[0]);
             GUILayout.Label(messages[i]);
             
         }
         GUILayout.EndScrollView();

I was attempting to see if i could constantly print Only one message.

Then, i could hold all these items in a string array and queue them up to re-print on the GUILayout.Label after clearing it.

So, u know...

fill array[0] with msg 0 queue.push(msg 0) Print msg 0

while(queue.count!=chatmax) { fill array[1] with msg 1 queue.push(msg 1) while(queue.count>0) queue.pop(); print msg 1 print msg 2 }

I can code this, i just need to get the logic straight

Aug 28, 2013 at 04:25 AM KuPAfoo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

I don't quite understand what your saying here... Kind of new to unity still, but i'm a quick learner.

The chat i have set up is half backwards! It starts in the top of the window when it should begin at the bottom when I send a new message it should appear above my send bar, with the next message appearing underneath, oldest message should be pushed upwards until out of sight and maybe deletion (15 messages currently)

lemme sample you some code:

  void OnGUI()
     {        
         GUILayout.BeginArea(new Rect(0, Screen.height - chatHeight, Screen.width-400, chatHeight));
         
         GUI.color = Color.gray;
         windowRect = GUI.Window (0, windowRect, WindowFunction, "");//draw chat window
         if (Event.current.type == EventType.keyDown && Event.current.character == '\n'){
             if (GUI.GetNameOfFocusedControl() == "ChatField")
             {                
                 SendChat(PhotonTargets.All);
                 lastUnfocusTime = Time.time;
                 GUI.FocusControl("");
                 GUI.UnfocusWindow();
             }
             else
             {
                 //if (lastUnfocusTime < Time.time - 0.1f)
                 //{
                     GUI.FocusControl("ChatField");
                 //}
             }
         }
         
          GUILayout.EndArea();
         
     }
 
     public static void AddMessage(string text)
     {
         SP.messages.Add(text);
         if (SP.messages.Count > 15)
             SP.messages.RemoveAt(0);
     }
 
 
     [RPC]
     void SendChatMessage(string text, PhotonMessageInfo info)
     {
         AddMessage("[" + info.sender + "] " + text);//display player name+msg
     }
 
     void SendChat(PhotonTargets target)
     {
         if (chatInput != "")
         {
             photonView.RPC("SendChatMessage", target, chatInput);
             chatInput = "";
         }
     }
 
     void SendChat(PhotonPlayer target)
     {
         if (chatInput != "")
         {
             chatInput = "[PM] " + chatInput;
             photonView.RPC("SendChatMessage", target, chatInput);
             chatInput = "";
         }
     }
 
     void OnLeftRoom()
     {
         this.enabled = false;
     }
 
     void OnJoinedRoom()
     {
         this.enabled = true;
     }
     void OnCreatedRoom()
     {
         this.enabled = true;
     }
     void WindowFunction (int windowID) {
         // Draw any Controls inside the window here
         GUI.BringWindowToBack(0);
         GUI.SetNextControlName("");
         
         
         //Show scroll list of chat messages
         scrollPos = GUILayout.BeginScrollView(scrollPos);
         
         GUI.color = Color.white;
         for (int i = messages.Count - 1; i >= 0; i--)
         {
             GUILayout.Label(messages[i]);
         }
         GUILayout.EndScrollView();
         //GUI.EndGroup();
         GUI.color = Color.white;
 
         //Chat input
        GUILayout.BeginHorizontal(); //Send button
         GUI.SetNextControlName("ChatField");
     chatInput = GUILayout.TextField(chatInput, GUILayout.MinWidth(200));
        
         
 
         if (GUILayout.Button("SEND", GUILayout.Height(17)))
            SendChat(PhotonTargets.All);
         GUILayout.FlexibleSpace();
         GUILayout.EndHorizontal();
 
more ▼

answered Aug 28, 2013 at 02:19 AM

KuPAfoo gravatar image

KuPAfoo
88 24 40 57

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x14
x6

asked: Aug 28, 2013 at 12:59 AM

Seen: 768 times

Last Updated: Aug 28, 2013 at 04:25 AM