Greetings,
I am currently having those errors :
I don’t understand. What does Unity expects from me ?
// Chat.js
var usingChat : boolean = false;
var showChat : boolean = false;
var inputField : String = "";
var scrollPosition : Vector2;
var width : int = 500;
var height : int = 100;
var playerName : String;
var lastUnfocusTime : float = 0.0;
var window : Rect;
var playerList : Array = new Array ();
var chatEntries : Array = new Array ();
var chatEntriesLength : int = 4;
function Start () {
window = Rect (Screen.width / 2 - width / 2, Screen.height - height + 5, width, height);
playerName = PlayerPrefs.GetString ("playerName", "");
if (playerName == "") playerName = "RandomName" + Random.Range (1, 999);
}
function OnConnectedToServer () {
ShowChatWindow ();
NetworkView.RPC ("TellServerOurName", RPCMode.Server, playerName);
AddGameChatMessage (playerName + " has joined the chat.");
}
function OnServerInitialized () {
ShowChatWindow ();
var newEntry : PlayerNode = new PlayerNode ();
newEntry.playerName = playerName;
newEntry.playerNetwork = Network.player;
playerList.Add (newEntry);
AddGameChatMessage (playerName + " has joined the chat.");
}
function GetPlayerNode (netPlay : NetworkPlayer) {
for (var entry : PlayerNode in playerList) {
if (entry.playerNetwork == netPlay)
return entry;
}
Debug.LogError ("GetPlayerNode : Requested a playernode of non-existing player.");
return null;
}
function OnPlayerDisconnected (netPlayer : NetworkPlayer) {
AddGameChatMessage ("A player has disconnected.");
playerList.Remove (GetPlayerNode (netPlayer));
}
function OnDisconnectedFromServer () {
CloseChatWindow ();
}
@RPC
function TellServerOurName (name : String, info : NetworkMessageInfo) {
var newEntry : PlayerNode = new PlayerNode ();
newEntry.playerName = playerName;
newEntry.playerNetwork = Network.player;
playerList.Add (newEntry);
AddGameChatMessage (playerName + " has joined the chat.");
}
function CloseChatWindow () {
showChat = false;
inputField = "";
chatEntries = new Array ();
}
function ShowChatWindow () {
showChat = true;
inputField = "";
chatEntries = new Array ();
}
function OnGUI () {
if (!showChat) return;
if (Event.current.type == EventType.keyDown (Event.current.character == "\n") inputField.length <=0) {
if (lastUnfocusTime + 0.25 < Time.time) {
usingChat = true;
GUI.FocusWindow (5);
GUI.FocusControl ("Chat input field");
}
}
window = GUI.Window (5, window, GlobalChatWindow, "");
}
function GlobalChatWindow (id : int) {
GUILayout.BeginVertical ();
GUILayout.Space (10);
GUILayout.EndVertical ();
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
for (var entry : ChatEntry in chatEntries) {
GUILayout.BeginHorizontal ();
if (entry.playerName == " - ")
GUILayout.Label (entry.playerName + entry.playerText);
else
GUILayout.Label (entry.playerName + " : "+ entry.playerText);
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 ();
lastUnfocusTime = Time.time;
}
}
}
function HitEnter (msg : String) {
msg = msg.Replace ("\n", "");
NetworkView.RPC ("ApplyGlobalChatText", RPCMode.All, playerName, msg);
}
@RPC
function ApplyGlobalChatText (name : String, msg : String) {
var entry : ChatEntry = new ChatEntry ();
entry.playerName = name;
entry.playerText = msg;
chatEntries.Add (entry);
if (chatEntries.length > chatEntriesLength)
chatEntries.Shift ();
scrollPosition.y = Mathf.Infinity;
inputField = "";
}
function AddGameChatMessage (str : String) {
ApplyGlobalChatText (" - ", str);
if (Network.connections.length > 0)
NetworkView.RPC ("ApplyGlobalChatText", RPCMode.Others, " - ", str);
}
@script RequireComponent (NetworkView)
// PlayerControl.js
var speed : float = 5;
function Start () {
if (!NetworkView.isMine)
enabled = false;
}
function Update () {
if (NetworkView.isMine) {
var moveDir : Vector3;
moveDir = Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
transform.Translate (speed * moveDir * Time.deltaTime);
}
}
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
if (stream.isWriting) {
var pos : Vector3 = transform.position;
stream.Serialize (pos);
} else {
var posRec = Vector3.zero;
stream.Serialize (posRec);
transform.position = posRec;
}
}