Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Sending a username to an instance to be displayed

Discussion in 'Multiplayer' started by degeneration, Oct 27, 2008.

  1. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Change 7/11/08

    I'm starting to get the hang off networking, but I'm having diffculty sending information to instances of the player when instantiated.

    I have a username that is submitted by the player before connection. This is stored as myUsername in a script called gui which handles the gui and conenctions.

    When a user connects an avatar is created, but I want the username to be displayed above the avatar in a Second Life / WoW style. Currently the GUI part for this is working, but I am having a lot of trouble sending the username by RPC to each user whenever a new player enters.

    Basically I need to do this.

    When a new avatar instantiates it requests the usernames of the other players and also sends its own name out.

    I think the issue I have been having is knwoing which instance has sent which name and applying it to the correct one.
     
  2. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    OK, just built a web player version, used my Mac's IP instead of 127.0.0.1, ran inside of Unity on Mac and opened the webplayer on my PC and it connected.

    Can a server not also be a client?
     
  3. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    You can not be a server and a client at the same time unfortunately.
     
  4. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    OK, I've got the network connect working, but without a simple networking tutorial that works (Could be my network...) I'm finding it a little hard to work out how to get people moving around.

    I can use Network.Instantiate to clone a prefab in the world.

    But I'm not sure how to go about creating new objects from prefabs to indicate other players inworld, which instantiate where that player is when they connect to the server, then move as other players move.

    Thanks to AngryAnt on IRC I know I need to use OnSerializeNewtorkView, which I think needs to be applied to the object I am cloning (that indicates other players, for this I'm just going to use a textured cube for now).

    Anything along the line of... use this class/function, here, here, here would help.

    So far on my little script I've gotten...(And I know I haven't dealt with disconnections yet...)

    Code (csharp):
    1. var textFieldString  = "App Started";
    2. var scrollViewVector: Vector2 = Vector2.zero;
    3. var playerCount: int = 0;
    4. var typingArea = "";
    5. var playerPrefab : Transform;
    6.  
    7. function OnGUI () {
    8.    
    9.     scrollViewVector = GUI.BeginScrollView(Rect(25,25,220,200), scrollViewVector,Rect(0,0,180,2000));
    10.     textFieldString = GUI.TextArea(Rect(0,0,200,2000 ),textFieldString);
    11.     GUI.EndScrollView();
    12.     typingArea = GUI.TextArea(Rect(25,225,200,100),typingArea);
    13.     if(GUI.Button(Rect (225,225,100,100),"Send")){
    14.         SendText();
    15.     }  
    16.     if(GUI.Button(Rect (425,0,150,50),"Launch Server")){
    17.         LaunchServer();
    18.     }
    19.     if(GUI.Button(Rect (425,50,150,50),"Connect")){
    20.         ConnectToServer(); 
    21.     }  
    22. }
    23.  
    24. function LaunchServer(){
    25.     textFieldString  = textFieldString + "\nCreating Server";
    26.     Network.incomingPassword = "";
    27.     Network.InitializeServer(32,25000);
    28. }
    29.  
    30. function OnServerInitialized(){
    31.     Debug.Log("Server initialized and ready");
    32.     textFieldString  = textFieldString + "\nServer Created";
    33. }
    34.  
    35. function ConnectToServer(){
    36.     textFieldString  = textFieldString + "\nConnecting to Server";
    37.     Network.Connect("147.197.166.113",25000,"");
    38. }
    39.  
    40. function OnConnectedToServer(){
    41.     textFieldString  = textFieldString + "\nConnected To Server";
    42.     Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    43. }
    44.  
    45. function OnPlayerConnected(player: NetworkPlayer){
    46.     textFieldString  = textFieldString + "\nPlayer" + playerCount++ + "connected from " + player.ipAddress + ":" + player.port;
    47. }
    48.  
    49. function SendText(){
    50.     //@RPC
    51.     textFieldString  = textFieldString + "\nSending Text";
    52.     networkView.RPC ("PrintText", RPCMode.All, typingArea);
    53.     typingArea  ="";
    54. }
    55.  
    56. @RPC
    57. function PrintText (text : String, info : NetworkMessageInfo)
    58. {
    59.    textFieldString  = textFieldString + "\n" + info.sender + " says:" + text;
    60. }
    61.  
    62.  
    63.  
    64.  
     
  5. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    I may have misunderstood your first post. Just to clarify, it is not possible to be a client and server within the same program; however, it is possible to run a client and server in 2 separate programs on the same computer.

    So you can create a server on your computer and connect to 127.0.0.1 from another unity program.

    Also, have you checked out the networking example project?
    http://unity3d.com/support/resources/example-projects/networking-example

    It isn't a tutorial, but it is pretty good. And you should be able to have Lerpz walking around without any problem on 2 instances of the program running on the same computer (1 client 1 server).

    Just remember, if you build as a web player, ensure that run in background is turned on or you will not be able to connect to the server.
     
  6. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Yeah, I've tried the networking example, but it doesn't want to run, although I am working form home today and the network is different from work.

    I can connect two machines together though so its more the process and flow of instanting avatars and moving them around.

    EDIT: Hmmm... the example seems to be working from home. Something to do with NAT punchthrough.

    Will now disassemble the example to see how it works.
     
  7. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Bump. 1st Post and title have been changed to reflect the new question. Don't want to flood this forum with my questions so am keeping it in one place.
     
  8. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    I think it is actually harder to follow the thread when the first post and title get changed to a different issue. I recommend starting a new thread for new issues in the future.

    What I do is create a file that contains global variables for the local user:
    Code (csharp):
    1. static var username : String = "Racer";
    Then when a network player is instantiated, I use the networkView.isMine property to set the name of all the networked versions of itself.

    Code (csharp):
    1. private var myName : String = "Unknown";
    2.  
    3. function OnNetworkInstantiate()
    4. {
    5.    if( networkView.isMine ){
    6.       networkView.RPC( "SetName", RPCMode.AllBuffered, Globals.username );
    7.    }
    8. }
    9.  
    10. @RPC
    11. function SetName( newName : String )
    12. {
    13.    myName = newName;
    14. }
     
  9. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Thanks Proton,

    Will set up an new thread from now on.

    To be honest that looks like something I've tried, but I can't look until Monday as the Mac is in the office, when Unity3d Windows arrives I won't have that problem :)

    I'll have a look Monday.
     
  10. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    It works!!!!

    I think the problem must have been to do with RPCMode.AllBuffered, I was using RPCMode.All

    Must do some more reading.

    Thanks Proton!