- Home /
Question by
thornekey · Mar 09, 2014 at 03:40 AM ·
networkingchat
Server (player) cant see the chat
Hey guys, i have this script. It is a chat script for multiplayer. Basically a player is the server and other players connect to the first player (again the 1st player is the server) and player 1 cant see chat, because theyre a server but player 2, and so on can.. :/
how can i fix it? im guessing it has something to do with the OnServerInititialized function.
using UnityEngine;
using System.Collections;
public class playerChat : MonoBehaviour {
bool usingChat = false;
bool showChat = false;
string inputField = "";
Vector2 scrollposition;
int width = 500;
int height = 400;
string playerName;
float lastUnfocusTime = 0;
Rect chatWindow;
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 () {
chatWindow = new Rect(Screen.width / 2 - width / 2, Screen.height - height + 5, width, height);
playerName = PlayerPrefs.GetString("playerName", "");
if(playerName == "") playerName = "Guest" + Random.Range(1,999);
}
void OnConnectedToServer() {
ShowChatWindow();
networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
addGameChatMessage(playerName + " has logged on!");
}
void OnServerInititialized() {
ShowChatWindow();
PlayerNode newEntry = new PlayerNode();
newEntry.playerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + " has logged on!");
}
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(playerName + " has disconnected!");
playerList.Remove(GetPlayerNode(netPlayer));
}
void OnDisconnectedFromServer() {
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 logged on!");
}
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 (lastUnfocusTime + .25f < Time.time)
{
usingChat = true;
GUI.FocusWindow (0);
GUI.FocusControl("Chat input field");
}
}
chatWindow = GUI.Window(0, chatWindow, 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) {
GUI.UnfocusWindow();
lastUnfocusTime = Time.time;
}
}
}
void HitEnter(string msg) {
msg = msg.Replace('\n', ' ');
networkView.RPC("ApplyGlobalChatText", 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);
}
}
Comment
Your answer
Follow this Question
Related Questions
Chat script in Networking 1 Answer
Receive value from UDP connection 0 Answers
Pun In Room Chat Script 1 Answer
How can I create a simple chat system for my multiplayer game? 1 Answer
Online Chat doesn't work 1 Answer