Author Topic: [Solved] Port Chat Example to UnityScript  (Read 2919 times)

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
[Solved] Port Chat Example to UnityScript
« on: October 18, 2013, 07:24:20 AM »
hey, im currently porting the chat example to unityscript.

according to http://forum.unity3d.com/threads/79760-How-to-use-generics-in-unity-javascript and http://forum.unity3d.com/threads/12090-Defining-structures-in-unity-javascript its possible to use generics for the list<> stuff  and classes insteat of struct.

but exacly this does an error when compiling. maybe i did something wrong.
this is the error i got:
  1.  Ambiguous reference 'List': System.Collections.Generic.List.<ChatEntry>, TNet.List.<ChatEntry>.
  1. Ambiguous reference 'List': System.Collections.Generic.List.<TNet.Player>, TNet.List.<TNet.Player>.
  1. //------------------------------------------
  2. //            Tasharen Network
  3. // Copyright © 2012 Tasharen Entertainment
  4. //------------------------------------------
  5.  
  6.  
  7. import System.Collections.Generic;
  8. import UnityEngine;
  9. import TNet;
  10.  
  11. /// <summary>
  12. /// This example script shows how to create a chat window powered by the Tasharen Network framework.
  13. /// You can see it used in Example Chat.
  14. /// </summary>
  15. class ChatEntry
  16. {
  17.         var text:String;
  18.         var color:Color;
  19. }
  20. //[ExecuteInEditMode]
  21.  
  22.         var mRect:Rect;
  23.         var mName:String = "Guest";
  24.         var mInput:String = "";
  25.         var ChatStyle:GUISkin ;
  26.  
  27.  
  28.         var mChatEntries: List.<ChatEntry>  = new List.<ChatEntry>();
  29.  
  30.         /// <summary>
  31.         /// Add a new chat entry.
  32.         /// </summary>
  33.  
  34.         function AddToChat (text:String, color:Color)
  35.         {
  36.                 var ent:ChatEntry = new ChatEntry();
  37.                 ent.text = text;
  38.                 ent.color = color;
  39.                 mChatEntries.Add(ent);
  40.         }
  41.  
  42.         /// <summary>
  43.         /// The list of players in the channel is immediately available upon joining a room.
  44.         /// </summary>
  45.  
  46.         function OnNetworkJoinChannel (success:boolean, error:String)
  47.         {
  48.                 mName = TNManager.playerName;
  49.  
  50.                 var text:String = "Players here: ";
  51.                 var players:List.<Player> = TNManager.players;
  52.                
  53.                 for (var i = 0; i < players.size; ++i)
  54.                 {
  55.                         if (i > 0) text += ", ";
  56.                         text += players[i].name;
  57.                         if (players[i].id == TNManager.playerID) text += " (you)";
  58.                 }
  59.                 AddToChat(text, Color.black);
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Notification of a new player joining the channel.
  64.         /// </summary>
  65.  
  66.         function OnNetworkPlayerJoin (p:Player)
  67.         {
  68.                 AddToChat(p.name + " has joined the channel.", Color.black);
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Notification of another player leaving the channel.
  73.         /// </summary>
  74.  
  75.         function OnNetworkPlayerLeave (p:Player)
  76.         {
  77.                 AddToChat(p.name + " has left the channel.", Color.black);
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Notification of a player changing their name.
  82.         /// </summary>
  83.  
  84.         function OnNetworkPlayerRenamed ( p:Player,  previous:String)
  85.         {
  86.                 AddToChat(previous + " is now known as " + p.name, Color.black);
  87.         }
  88.  
  89.         /// <summary>
  90.         /// This is our chat callback. As messages arrive, they simply get added to the list.
  91.         /// </summary>
  92.  
  93.         @RFC
  94.         function OnChat (playerID:int, text:String)
  95.         {
  96.                 // Figure out who sent the message and add their name to the text
  97.                 var player:Player = TNManager.GetPlayer(playerID);
  98.                 var color:Color = (player.id == TNManager.playerID) ? Color.green : Color.white;
  99.                 AddToChat("[" + player.name + "]: " + text, color);
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Send the typed message to the server and clear the text.
  104.         /// </summary>
  105.  
  106.         function Send ()
  107.         {
  108.                 if (!String.IsNullOrEmpty(mInput))
  109.                 {
  110.                         tno.Send("OnChat", Target.All, TNManager.playerID, mInput);
  111.                         mInput = "";
  112.                 }
  113.         }
  114.  
  115.         /// <summary>
  116.         /// This function draws the chat window.
  117.         /// </summary>
  118.  
  119.         function OnGUI ()
  120.         {
  121.                 if(ChatStyle)
  122.                 {
  123.                         GUI.skin=ChatStyle;
  124.                 }
  125.                
  126.                 //float cx = Screen.width * 0.5f;
  127.                 //float cy = Screen.height * 0.5f;
  128.  
  129.         //      GUI.Box(new Rect(Screen.width * 0.5f - 270f, Screen.height * 0.5f - 200f, 540f, 410f), "");
  130.  
  131.         /*      GUI.Label(new Rect(cx - 140f, cy - 170f, 80f, 24f), "Nickname");
  132.                 GUI.SetNextControlName("Nickname");
  133.                 mName = GUI.TextField(new Rect(cx - 70f, cy - 170f, 120f, 24f), mName);
  134.  
  135.                 if (GUI.Button(new Rect(cx + 60f, cy - 170f, 80f, 24f), "Change"))
  136.                 {
  137.                         // Change the player's name when the button gets clicked.
  138.                         TNManager.playerName = mName;
  139.                 }*/
  140.  
  141.                 GUI.SetNextControlName("Chat Window");
  142.                 mRect = new Rect(0, Screen.height-400, 400f, 300f);
  143.                 GUI.Window(0, mRect, OnGUIWindow, "");
  144.  
  145.                 if (Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyUp)
  146.                 {
  147.                         var ctrl:String = GUI.GetNameOfFocusedControl();
  148.  
  149.                         if (ctrl == "Nickname")
  150.                         {
  151.                                 // Enter key pressed on the input field for the player's nickname -- change the player's name.
  152.                                 TNManager.playerName = mName;
  153.                                 GUI.FocusControl("Chat Window");
  154.                         }
  155.                         else if (ctrl == "Chat Input")
  156.                         {
  157.                                 Send();
  158.                                 GUI.FocusControl("Chat Window");
  159.                         }
  160.                         else
  161.                         {
  162.                                 // Enter key pressed -- give focus to the chat input
  163.                                 GUI.FocusControl("Chat Input");
  164.                         }
  165.                 }
  166.         }
  167.  
  168.         /// <summary>
  169.         /// This function draws the chat window and the chat messages.
  170.         /// </summary>
  171.  
  172.         function OnGUIWindow (id:int)
  173.         {
  174.                 GUI.SetNextControlName("Chat Input");
  175.                 mInput = GUI.TextField(new Rect(6, mRect.height - 30, 328, 24), mInput);
  176.  
  177.                 if (GUI.Button(new Rect(334, mRect.height - 31, 60, 26), "Send"))
  178.                 {
  179.                         Send();
  180.                         GUI.FocusControl("Chat Window");
  181.                 }
  182.  
  183.                 GUI.BeginGroup(new Rect(2, 20, 382, 254));
  184.                 //{
  185.                         var rect:Rect = new Rect(4, 244, 382, 300);
  186.  
  187.                         for (var i = mChatEntries.size; i > 0; )
  188.                         {
  189.                                 var ent:ChatEntry = mChatEntries[--i];
  190.                                 rect.y -= GUI.skin.label.CalcHeight(new GUIContent(ent.text), 382);
  191.                                 GUI.color = ent.color;
  192.                                 GUI.Label(rect, ent.text, GUI.skin.label);
  193.                                 if (rect.y < 0) break;
  194.                         }
  195.                         GUI.color = Color.white;
  196.                 //}
  197.                 GUI.EndGroup();
  198.         }
  199.  
  200.  


-----------------------------------
Solved:

Problem was both ( TNet and System.Collections.Generic ) has a List<>. this caused the error.
i deletet the import of System.Collections.Generic and only worked with System.Collections.Generic.List.<ChatEntry>  instead.
« Last Edit: October 18, 2013, 08:55:29 AM by ckrin »