- Home /
CHAT Multiplayer Problem
Hi there!
I've faced with annoying problem wich happens when players try to send message via chat. Every time when players send message, other's can't synhronize message typer's data like his name.. and their's own position, where this message is going to be located in chat. So here's full script of my chat system and Screenshot representing my errors:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChatMaster : MonoBehaviour
{
public GUISkin skin;
bool enabled;
class ChatEntry
{
public string name = "";
public string message = "";
}
ArrayList entries;
Vector2 currentScrollPos = new Vector2();
string inputField = "";
bool chatInFocus = false;
string inputFieldFocus = "CIFT";
bool absPos = false;
void Awake()
{
InitializeChat();
AddChatEntry("-SystemMessage-");
}
void InitializeChat()
{
entries = new ArrayList();
unfocusChat();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.C))
enabled = !enabled;
}
void OnGUI()
{
GUI.skin = skin;
if (enabled)
{
Draw();
}
}
//draw the chat box in size relative to your GUIlayout
public void Draw()
{
ChatWindow();
}
void ChatWindow()
{
if (networkView.isMine)
{
GUI.BeginGroup(new Rect(0, 100, 500, 750));
GUILayout.BeginVertical(skin.FindStyle("Window"));
GUILayout.Label("Спутниковая связь", skin.FindStyle("textStyle2"));
currentScrollPos = GUILayout.BeginScrollView(currentScrollPos, skin.FindStyle("scrollview"), GUILayout.MaxWidth(300), GUILayout.MinWidth(100), GUILayout.MaxHeight(500)); //limits the chat window size to max 1000x1000, remove the restraints if you want
foreach (ChatEntry ent in entries)
{
GUILayout.BeginHorizontal(skin.FindStyle("Box"));
GUI.skin.label.wordWrap = true;
GUILayout.Label(ent.name + ent.message);
GUILayout.EndHorizontal();
GUILayout.Space(3);
}
}
GUILayout.EndScrollView();
if (chatInFocus)
{
GUI.SetNextControlName(inputFieldFocus);
inputField = GUILayout.TextField(inputField, skin.FindStyle("textfield"), GUILayout.MaxWidth(300), GUILayout.MinWidth(100));
GUI.FocusControl(inputFieldFocus);
}
GUILayout.EndVertical();
GUI.EndGroup();
if (chatInFocus)
{
HandleNewEntries();
}
else
{
checkForInput();
}
}
void unfocusChat()
{
//Debug.Log("unfocusing chat");
inputField = "";
chatInFocus = false;
}
void checkForInput(){
if(Event.current.type == EventType.KeyDown && Event.current.character == '\n' && !chatInFocus){
GUI.FocusControl(inputFieldFocus);
chatInFocus = true;
currentScrollPos.y = float.PositiveInfinity;
}
}
void HandleNewEntries()
{
if (Event.current.type == EventType.KeyDown && Event.current.character == '\n')
{
if (inputField.Length <= 0)
{
unfocusChat();
return;
}
if (inputField.Length < 90)
{
networkView.RPC("AddChatEntry", RPCMode.All, this.GetComponent<Player>().characterName, inputField);
}
//AddChatEntry("Cookie monster", inputField); //for offline testing
unfocusChat();
//Debug.Log("unfocusing chat and entry sent");
}
}
[RPC]
public void AddChatEntry(string name, string msg)
{
HandleRemovingEntries();
ChatEntry newEntry = new ChatEntry();
newEntry.name = name + ": "; //For futher displaying in chat
newEntry.message = msg;
entries.Add(newEntry);
currentScrollPos.y = float.PositiveInfinity;
}
public void AddChatEntry(string msg)
{
HandleRemovingEntries();
ChatEntry newEntry = new ChatEntry();
newEntry.name = "";
newEntry.message = msg;
entries.Add(newEntry);
currentScrollPos.y = float.PositiveInfinity;
}
[RPC]
void RemoveChatEntry()
{
entries.RemoveAt(0);
}
void HandleRemovingEntries()
{
if (entries.Count > 300)
{
networkView.RPC("RemoveChatEntry", RPCMode.All);
}
}
}
Help me please to locate "if(networkdView.IsMine)" and ".RPCs()" where it's need. I've tried a lot of combinations of it, yet it've brought no results. Hope very much for your responds...
Answer by tobiass · Jul 10, 2015 at 09:06 AM
With RPCs, you can implement in-room chat only.
That said, please take a look at the Demo "Worker" in the PUN package for a simple but working implementation of RPCs for Chat.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer - Opposite names above players 1 Answer
Network Chat not sending messages to other clients 1 Answer
networkView not called on Child objects? 0 Answers
Networking Unknown message ID 0 Answers