- Home /
Online Chat doesn't work
Hello all iv been in the making for a game called war season its an Online fps and just last night i was looking at a tutorial on how do make a basic muytiplayer the connection and character spwaning works fine its just the chat i finished the chat script but it has a bug and the script look fine can any help me if im missing somthing or somthing is wrong
this is the script
using UnityEngine; using System.Collections;
public class Chat : MonoBehaviour {
bool usingChat = false;
bool showChat = false;
string inputField = "";
Vector2 ScrollPosition;
int width = 500;
int height = 100;
string playerName;
float LastOnFocusedtime = 0;
Rect window;
ArrayList playerList = new ArrayList ();
class PlayerNode
{
public string PlayerName;
public NetworkPlayer player;
}
ArrayList chatEntries = new ArrayList();
class ChatEntry
{
public string name = "";
public string text = "";
}
// Use this for initialization
void Start ()
{
window = new Rect(Screen.width /2 - width /2, Screen.height - height + 5, width, height);
playerName = PlayerPrefs.SetString("Playername", "");
if(playerName == "") playerName = "RandomName" + Random.Range(1, 999);
}
void OnConnectedToServer()
{
ShowChatWindow();
networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
addGameChatMessage(playerName + " Have just joined the chat");
}
void OnServerInitialized()
{
showChatWindow();
PlayerNode newEntry = new PlayerNode();
newEntry.PlayerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + " Have just joined the chat");
}
PlayerNode GetPlayerNode(NetworkPlayer netPlay)
{
foreach (PlayerNode entry in playerList)
{
if(entry.player == netPlay)
return entry;
}
Debug.LogError("GetPlayerNode: Requested a playernode of non-existing player!");
return null;
}
void OnPlayerDisconnected(NetworkPlayer netplayer)
{
addGameChatMessage("A player has disconected");
playerList.Remove(GetPlayerNode(netplayer));
}
void OnDisconectedFromServer()
{
CloseChatWindow();
}
[RPC]
void TellServerOurName(String name, NetworkMessageInfo info)
{
PlayerNode newEntry = new PlayerNode();
newEntry.PlayerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + "has joined the Chat!");
}
void CloseChatWindow()
{
showChat = false;
inputField = "";
chatEntries = new ArrayList();
}
void ShowChatWindow()
{
showChat = true;
inputField = "";
chatEntries = new ArrayList();
}
void OnGUi()
{
if(!showChat) return;
if(Event.current.type == EventType.keyDown && Event.current.character == '\n' & inputField.Length <=0)
{
if(LastOnFocusedtime + .25f < Time.time)
{
usingChat = true;
GUI.FocusWindow(5);
GUI.FocusControl("Chat input field");
}
}
window = GUI.Window(5, window, GlobalChatwindow, "");
}
void GlobalChatWindow(int id)
{
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.EndVertical();
ScrollPosition = GUILayout.BeginScrollView(ScrollPosition);
foreach (ChatEntry entry in chatEntries)
{
GUILayout.BeginHorizontal();
if(entry.name == " - ")
GUILayout.Label(entry.name + entry.text);
else
GUILayout.Label(entry.name + ": " + entry.text);
GUILayout.EndHorizontal();
GUILayout.Space(2);
}
GUILayout.EndScrollView();
if(Event.current.type == EventType.keyDown && Event.current.character == '\n' & inputField.Length > 0)
HitEnter(inputField);
GUI.SetNextControlName("Chat input field");
inputField = GUILayout.TextField(inputField);
if(Input.GetKeyDown("mouse 0"))
{
if(usingChat)
{
usingChat = false;
GUI.UnfocusWindow();
LastOnFocusedtime = Time.time;
}
}
void HitEnter (string msg)
{
msg = msg.Replace('\n', ' ');
networkView.RPC("ApplyGobalChatText", RPCMode.All, playerName, msg);
}
[RPC]
void ApplyGlobalChatText(String name,String msg)
{
ChatEntry entry = new ChatEntry();
entry.name = name;
entry.text = msg;
chatEntries.Add(entry);
if(chatEntries.Count > 4)
chatEntries.RemoveAt(0);
ScrollPosition.y = 1000000;
inputField = "";
}
void addGameChatMessage(string str)
{
ApplyGlobalChatText(" . ", str);
if (Network.connections.Length > 0);
networkView.RPC("ApplyGlobalChatText", RPCMode.Others, " . ", str);
}
}
}
this is were its not working right it says im missing something in this line
void HitEnter (string msg)
{
msg = msg.Replace('\n', ' ');
networkView.RPC("ApplyGobalChatText", RPCMode.All, playerName, msg);
}
so if any one can help please do thanks!
Answer by superpig · May 08, 2011 at 12:24 AM
Typo, you fool! The string
"ApplyGobalChatText"
should be
"ApplyGlobalChatText"
You're missing an l.
How the hell did you manage to spot that? I'm impressed xD
This is why good, clear, consistent, correct na$$anonymous$$g is important ;-)
Ahhh!! Dammit I still barely noticed it thanks allot man :)