- Home /
How to destroy a chat message (GUI label)?
Basically i'm working on a GUI and it has a lobby with networking, so others can connect. I used snippets of code from other places to make an addition to my networking lobby that allows people to type and chat between themselves, also notifying when people join or leave the lobby. The only problem is that once you leave the lobby or close the server, then find a new lobby or create a new server, every bit of chat from the last one is still there inside the scrollview. I want each message to either delete or fade away after about 15 seconds so that this problem doesn't occur, and so that the scrollview doesn't get very full. Here is a bit of my code...
private void ChatFunc(int id)
{
if(mySkin)
{
GUI.skin = mySkin;
}
Scrollposition = GUILayout.BeginScrollView(Scrollposition);
GUILayout.Label(OutputText);
GUILayout.EndScrollView();
GUILayout.Space(50);
GUILayout.BeginHorizontal();
InputText = GUILayout.TextField(InputText, 50);
if (Event.current.keyCode == KeyCode.Return)
{
if(InputText.Length > 0)
{
networkView.RPC("SendMsg", RPCMode.All, MultiplayerManager.instance.PlayerName + ": " + InputText);
Scrollposition.y += 100;
InputText = "";
}
}
GUILayout.EndHorizontal();
}
[RPC]
private void SendMsg(string msg)
{
OutputText += msg + "\n";
}
`
Here is also the line that initialises the chat when you join the lobby (inside ongui)...
if(CurrentMenu == "Lobby")
{
ChatRect = GUI.Window(0,ChatRect,ChatFunc, "");
}
Finally here are the other bits that are useful...
private Vector2 Scrollposition;
private Rect ChatRect = new Rect(1200,710,1300,700);
Ideas anyone? This would really mean a lot if you provide a solution!
Answer by Chronos-L · Jul 27, 2013 at 12:17 PM
The only problem is that once you leave the lobby or close the server,
Clean your OutputText
right before you do either of the following:
leave the lobby or close the server
find a new lobby or create a new server (You will have the same chat if you go back to the last lobby you are in)
I want each message to either delete or fade away after about 15 seconds so that this problem doesn't occur
A better approach would be limiting the number of message-piece you have in the chat-box.
private Queue<string> chat;
...
//Put this in an appropriate location
if( chat != null ) {
chat.Clear();
}
else {
chat = new Queue<string>();
}
[RPC]
private void SendMsg(string msg)
{
//Insert the message into a Queue (First in first out)
chat.Enqueue( msg );
while( chat.Count > 10 ) {
//Delete the older message when there are
//more than 10 messages
chat.Dequeue();
}
}
private void ChatFunc(int id)
{
...
Scrollposition = GUILayout.BeginScrollView(Scrollposition);
foreach( string s in chat ) {
GUILayout.Label( s );
}
GUILayout.EndScrollView();
...
}
Thanks for the in-depth response! Could you maybe sort of explain how I would implement this into my code? I would like it to be in a certain position in the lobby as well.
But he did tell you where to implement it into your code, just look above the code snippet.
Just to clarify, Queue chat
is equivalent to your OutputText
in your older code.
You should put this part (it is just cleaning the variable storing the messages, it doesn't matter whether it is a string or a queue) whenever you join/create a new server:
//Put this in an appropriate location
if( chat != null ) {
chat.Clear();
}
else {
chat = new Queue<string>();
}
All these in the below are just a simple modification of your current code, just keep them where they were:
[RPC]
private void Send$$anonymous$$sg(string msg)
{
...
}
private void ChatFunc(int id)
{
...
}
Cool, so far no errors. The only thing now is how to make it visible again. Before, I had a window that contained the whole thing, which was set by a private Rect, and was what showed the whole thing when the lobby opened. Now with your new bit //Put in the appropriate location it doesn't show up because there is nothing to contain the contents. Any ideas?
Please note that these modification/addition will not affect your previous working code, the answer I provided will enhance the behaviour while building on your working code. Regarding the missing GUI element, this should do:
private void ChatFunc(int id)
{
...
Scrollposition = GUILayout.BeginScrollView(Scrollposition);
foreach( string s in chat ) {
GUILayout.Label( s );
}
if( chat.Count == 0 ) {
//You will get a empty label when you have nothing in the queue
GUILayout.Label( "" );
}
GUILayout.EndScrollView();
...
}
Your answer
Follow this Question
Related Questions
BeginScrollView question on auto increasing 1 Answer
GUILayout Sort Problem 1 Answer
Trouble using C# tooltips 1 Answer
Change Color For One Line? 1 Answer