Search Unity

Where is GUI() called in this script?

Discussion in 'Multiplayer' started by reset, Jun 9, 2009.

  1. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Here is a Chat script I adapted from a C# script to JavaScript - and dragged it onto a Game Object. Also add a Network View to the Game Object!

    It works!!

    However I am new to JavaScript and I just wanted ask a couple of things

    1. the function GUI() is called - but from where?
    2. is the script ok for a first attempt? are there thing wrong with it?

    Any help would be great.




    // Proclaim the variables to be used at the start
    var ChatWindowPos;
    var ConnWindowPos;
    var natCapable;
    var polllist = false;
    var plist;
    var MessageToSend;
    var scrollPosition;
    var netconn;
    var ChatHistoryArray;
    var MessHist;

    // when program starts assign some values to some of the variables
    function Start()
    {
    scrollPosition =Vector2.zero;
    ChatHistoryArray = new Array ();
    MessageToSend = "type message here";
    natCapable = ConnectionTesterStatus.Undetermined;
    }

    // this function sets up the two interface windows and calls their respective functions
    function OnGUI()
    {
    ChatWindowPos = GUI.Window (0, Rect(0, 0, 300, 400), ChatWindow, "Chat Window");
    ConnWindowPos = GUI.Window(1, Rect(301, 0, 200, 280), ConnWindow, "Network Connection");
    }

    // RPC tag
    @RPC

    // this is the function that updates the Chat History array
    function SendMess (MessHist : String)
    {
    ChatHistoryArray.Reverse();
    ChatHistoryArray.Push(MessHist);
    ChatHistoryArray.Reverse();
    }

    // chat window function
    function ChatWindow (windowID : int)
    {
    GUILayout.Label("Chat History");
    GUILayout.BeginVertical();
    scrollPosition = GUILayout.BeginScrollView (
    scrollPosition, GUILayout.Width (280), GUILayout.Height (200));

    for(var i:int=0; i
    GUILayout.Label(ChatHistoryArray); }

    GUILayout.EndScrollView();

    MessageToSend = GUILayout.TextArea(MessageToSend);

    if (GUILayout.Button("Send")) {networkView.RPC("SendMess", RPCMode.All, MessageToSend); MessageToSend = "";}

    GUILayout.EndVertical();
    GUI.DragWindow ();
    }

    // Connection window function
    function ConnWindow (windowID : int)
    {
    GUILayout.Label(natCapable.ToString());

    if (GUILayout.Button("Test NAT")) {
    natCapable = Network.TestConnection();}

    GUILayout.Label("PeerType: ");

    if (GUILayout.Button("Start Server")){
    Network.useNat = true;
    Network.InitializeServer(6, 25002);}

    if (GUILayout.Button("Register with Master Facilitator")){
    MasterServer.RegisterHost("Name1", "Name2");}

    if (GUILayout.Button("Check Host List") ){
    MasterServer.ClearHostList();
    MasterServer.RegisterHost("Name1", "Name2");}

    if (GUILayout.Button("Disconnect")){
    Network.Disconnect(200);}

    if (MasterServer.PollHostList().length != 0) {
    var hostData: HostData[] = MasterServer.PollHostList();
    for (var i:int=0; i
    GUILayout.Label("Game name: " + hostData.gameName);
    GUILayout.Label("Players: " + hostData.connectedPlayers.ToString() + "/" + hostData.playerLimit.ToString());
    if (GUILayout.Button("Connect") ){
    Network.useNat = true;
    MasterServer.RegisterHost("Name1", "Name2");
    Network.Connect(hostData.ip, hostData.port);
    }
    }

    }
    }

    // Updates the request for this chat prog on the MS
    function Update()
    {
    MasterServer.RequestHostList("Name1");
    }
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  3. bernieoneill

    bernieoneill

    Joined:
    Aug 11, 2009
    Posts:
    3