Hep! Display what you have entered in chat window

Hi, now I’m currently working on chat window,
and I managed to find following code that implements chat window:

public var commandLine:String = ""; 
function OnGUI () { 
   if (Event.current.Equals (Event.KeyboardEvent ("return")) ) 
            { 
         print("return pressed"); 
         isTalking = !isTalking; 
   } 
   if(isTalking){ 
         GUI.SetNextControlName ("ChatField"); 
         commandLine = GUI.TextField (Rect (10, 80, 600, 20), commandLine, 25); 
         GUI.FocusControl ("ChatField"); 
         } 
    

   if (commandLine!=""  isTalking == false) 
         { 
            // What now?!
         } 
   }

This is typical wow style chat window, where if you press enter, chat window appears, and press it again to make it disappear.

But thing it passes message typed to nowhere!

I’m pretty sure i have to do something in //What now?! part, but since it’s OnGUI() function so if I try to call any function that displays variable “commandLine”, it prints that out every frame, not just once.

Also, I want to make some kind of chat history, so it displays up to last 10 lines that user typed. May be I have to use some arrays, but I’m so noob.

Could anyone help?!

it will only print it once actually if you reset commandLine to “” after sending it, which you are likely wanting to do as you want to send it to the server there and to the chat history

Take this snippet. It has:

  • text with shadow
  • timed delete of text
  • command handling
  • networking
  • i just cut it together from my blobfoot chat-script, perhaps there are one or two errors :wink:
#pragma strict

public var shadowDist : int = 1;
public var activeChatLines : int = 16;
public var chatLineTimeout : float = 30.0;
public var chatfont : Font;

public var playerName : String = "unitygirl";

// chat
private var maxChatTextLen : int = 96;
private var chatText : String = "";

private var chatEntries : Array = Array();
private var chatEntryTimeouts : Array = Array();

private var bottomLeftBox : Rect = Rect (44, Screen.height-60, 800, 20);
private var shadowChatBox : Rect = bottomLeftBox;
private var activeChatBox : Rect = bottomLeftBox;

private var activeChatString : String = "";

private var inGameChatActive : boolean = false;

//private var lineHeight : int = 13;

private var gs : GUIStyle;

function Start()
{
	gs = new GUIStyle();
	gs.font = chatfont;
}

function Update()
{
	if(Input.GetKeyDown (KeyCode.Return))
	{
		if(inGameChatActive  chatText!="")
		{
			// commands start with "/" like "/name Ethan"
			if(chatText.StartsWith("/"))
			{	
				var spaceIndex : int = chatText.IndexOf(" ", 0, chatText.length);
				var commandName : String = "";
				var commandValue : String = "";
				
				if(spaceIndex==-1)
				{		
					commandName = chatText.Substring(1, chatText.length-1);
				}
				else
				{	
					commandName = chatText.Substring(1, spaceIndex-1);
					commandValue = chatText.Substring(spaceIndex+1, chatText.length - (spaceIndex+1));		
				}
				
				//Debug.Log("- -" + commandName + "- -" + commandValue + "- -");
				ParseChatCommand(commandName, commandValue);
				
			}
			else
			{
				// send text and close chat if it isnt a command
				chatText = playerName + ":  " + chatText;
				AddChatEntry(chatText);
				networkView.RPC("AddChatEntry", RPCMode.Others, chatText);
			}	
			chatText = "";
		}
		
		// switch it
		inGameChatActive = !inGameChatActive;
	}
	
	// did oldest chat element ran out of lifetime?
	if(chatEntryTimeouts.length > 0  Time.time > System.Convert.ToDouble(chatEntryTimeouts[0]))
	{
		chatEntries.RemoveAt(0);
		chatEntryTimeouts.RemoveAt(0);
		
		RebuildChatString();
	}
}

function OnGUI()
{
	gs.normal.textColor = Color(0.0, 0.0, 0.0);
	GUI.Label(shadowChatBox, activeChatString, gs);

	gs.normal.textColor = Color(1.0, 1.0, 0.0);
	GUI.Label(activeChatBox, activeChatString, gs);
	
	if(inGameChatActive)
	{
		GUI.FocusControl("chatInput");
		
		GUI.SetNextControlName("chatInput");
		chatText = GUI.TextField(bottomLeftBox, chatText, maxChatTextLen);
	}
}

@RPC // clients + server
public function AddChatEntry(str : String)
{
	audio.Play();
	
	chatEntries.Add(str);
	chatEntryTimeouts.Add(Time.time + chatLineTimeout);

	if(chatEntries.length > activeChatLines)
	{
		chatEntries.RemoveAt(0);
		chatEntryTimeouts.RemoveAt(0);
	}
		
	RebuildChatString();
}

function RebuildChatString()
{		
	var i : int = 0;
	
	activeChatString = "";

		
	// WRITE ACTIVE CHAT LABEL TEXT 
	activeChatBox.height = (chatEntries.length+1) * gs.lineHeight;
	activeChatBox.y = bottomLeftBox.y - activeChatBox.height;
	
	shadowChatBox = activeChatBox;
	shadowChatBox.y += shadowDist;
	shadowChatBox.x += shadowDist;
	
	for(i = 0; i<chatEntries.length; ++i)
	{
		activeChatString += chatEntries[i] + "\n";
	}
}

function ParseChatCommand(commandName : String, commandValue : String)
{	
	switch(commandName)
	{		
		case "name":
		
			// TODO do sth with command /name
			
			break;
			
		default:
		
			// TODO
			
			break;
	}
}

thanks for posting a great chat script from your game… but there’s unknown identifier ‘global’, so is there some extra work that I need to do?

I’m not exactly sure what this global is and what it does…

if you don’t mind, could you please add some more comments describing your script? like what each .Something does… :S

Although I prefer to do something with my original script posted, at least I have some understanding with that script… :wink:

oops, sorry i cant test the script myself because i dont have access to unity from here.

i just replaced the script from above, just try again, now all Global. vars are replaced by local ones.

updated script from above

Thanks, I kinda managed to change all of these global thingy to local variables - so that’s fine - Thanks for updating the script tho. (Now I can compare so to check whether I’ve done it correctly!)

By the way - as you know the purpose of the chat is to chat online - right? is there any additional script or tweaks to make it kinda multiplayer?

As you can see here:

....
AddChatEntry(chatText);
networkView.RPC("AddChatEntry", RPCMode.Others, chatText); 
...

It is already multiplayer! You just have to add a networkview to the gameobject which contains this script.
Ah and a AudioSource, because i am playing a sound everytime a new chatline appears :wink:

Oh! I see. So that’s was the part that does network thingy! You are awesome!

I think I saw that kind of Network view and RPC thingy somewhere… among the hundreds of tutorials I have :S

Still, may be I have to study networking lot more… since I don’t know what to do next :frowning:
(I KNOW NOTHING ABOUT NETWORKING - and the most of scripting I do not know either (what do I know?!))

I think it’s too hard for me to turn my little game into multiplayer game! T-T

btw, uncommenting RPC part, it gives me error like this:

[/code]

uncomment the
networkView.RPC(“AddChatEntry”, RPCMode.Others, chatText);
line, and first use it if you are client or server of an existing connection.

you should start with the network tutorial example :slight_smile:
It has a small network chat, too.