Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

playerID increment when clients join

Discussion in 'Multiplayer' started by filecity, Dec 2, 2009.

  1. filecity

    filecity

    Joined:
    Apr 21, 2009
    Posts:
    117
    Hi i have a question here.
    I am making a multiplayer game, using server and client.

    playerID for server is 0, while other clients which join the game increases their playerID by 1. Is it necessary to send RPC function or is there any better methods? Thank You.

    Code (csharp):
    1.  
    2. var playerID : int = 0;
    3. function Spawn()
    4. {
    5.  
    6. if(Network.isServer)
    7. {
    8. playerID = 0;
    9. }
    10. else if(Network.isClient)
    11. {
    12. playerID++;
    13. }
    14.  
    15. }
    16.  
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    every player that connections has a NetworkPlayer object automatically assigned with a unique id
    you should use that one.
     
  3. filecity

    filecity

    Joined:
    Apr 21, 2009
    Posts:
    117
    hi again, i have tried the code below. The playerID should sent back to server and update the playerID again. but it doesn't work. any idea why is it so? thanks.

    Code (csharp):
    1.  
    2. var currentPlayerID : int;
    3. var networkPlayer : NetworkPlayer;
    4.  
    5. function OnConnectedToServer()
    6. {
    7.    networkView.RPC( "RequestOwner", RPCMode.Server );
    8. }
    9.  
    10. @RPC
    11. function RequestOwner( info : NetworkMessageInfo )
    12. {
    13.    networkView.RPC( "SetOwner", info.sender, info.sender );
    14. }
    15.  
    16. @RPC
    17. function SetOwner( a_owner : NetworkPlayer )
    18. {
    19.    networkPlayer = a_owner;
    20.    currentPlayerID = parseInt( networkPlayer.ToString() );
    21. }
    22.  
     
  4. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    But how?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    @DIProgan: Check the script reference on class Network and check its fields. That will show you :)
     
  6. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Only been doing that for x hours :p Edit: Just maaybe this NetworkMessageInfo thingy can help out.... Edit2: Yup finaly it seems ^^
     
  7. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Code (csharp):
    1. @RPC
    2. function DoneTyping(text : String, info : NetworkMessageInfo){
    3.  
    4. number=parseInt(info.sender);
    5. Arr[number]=text;
    6. }
    7.  
    How do I convert the info.sender to a usable int for my array?
     
  8. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Sollution was int.Parse(info.sender.ToString())
     
  9. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429

    Any idea how to disconnect a player by referencing their NetworkPlayer object's unique ID?
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Network.CloseConnection actually expects that you hand it a NetworkPlayer
    it does not accept anything else