Search Unity

Getting the NetworkPlayer variable

Discussion in 'Multiplayer' started by Doug, Dec 5, 2007.

  1. Doug

    Doug

    Joined:
    Oct 19, 2007
    Posts:
    36
    I am trying to get the NetworkPlayer variable for the currently running Unity program.

    It seems to work okay on the Mac, but not Windows:

    Code (csharp):
    1. var myNetworkPlayer : NetworkPlayer;
    2.  
    3. function StartServer(){
    4.    Network.InitializeServer( 32, 8000 );
    5. }
    6.  
    7. function StartClient( serverIP, serverPort ){
    8.    Network.Connect( serverIP, serverPort );
    9. }
    10.  
    11. function OnServerInitialized(){
    12.    myNetworkPlayer = networkView.owner;
    13. }
    14.  
    15. function OnConnectedToServer(){
    16.    myNetworkPlayer = networkView.owner;
    17. }
    There is a NetworkView attached to the GameObject with that script. And the script is being observed by the NetworkView.

    The error I get in Windows is:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.NetworkView.get_owner()
    3. NetworkLogic.OnServerInitialized()
    4. UnityEngine.Network:InitializeServer( Int32, Int32 )
    5. NetworkLogic:StartServer()
    6. MainMenuGUI:ServerWindow(Int32)
    7. UnityEngine.GUI:BeginWindows(Event, Int32)
    Same type of error for the client as well. But the code seems to work on Mac. Any ideas?
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    What you are doing there is getting the network view owner of the network view attached to the same object as the script. This is most likely owned by the server, i.e. has a scene network view ID. So myNetworkPlayer should in all cases (client or server) be set to the server NetworkPlayer.

    You can get the NetworkPlayer of the connecting party (remote) in OnPlayerConnected and OnConnectedToServer, this is shown in the example code for those functions. However, there is currently no specific function for getting the local NetworkPlayer instance.

    But this shouldn't be crashing the windows player so I'll look into that, might be a bug.
     
  3. Doug

    Doug

    Joined:
    Oct 19, 2007
    Posts:
    36
    Each client(and the server) own their own version of that GameObject. I created a little workaround that fits my needs:

    Code (csharp):
    1. function OnConnectedToServer()
    2. {
    3.    networkView.RPC( "RequestOwner", RPCMode.Server );
    4. }
    5.  
    6. @RPC
    7. function RequestOwner( info : NetworkMessageInfo )
    8. {
    9.    networkView.RPC( "SetOwner", info.sender, info.sender );
    10. }
    11.  
    12. @RPC
    13. function SetOwner( a_owner : NetworkPlayer )
    14. {
    15.    networkPlayer = a_owner;
    16.    currentPlayerID = parseInt( networkPlayer.ToString() );
    17. }
    Kind of a roundabout way of finding each client(or server) NetworkPlayer, but it gets the job done.

    The reason I needed to do it is because the server is authoritative. So all player GOs are owned by the server. Each player has an extra NetworkPlayer variable to say which GO is 'theirs' locally.
     
    BlackPanda likes this.
  4. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    Thanks Doug, I was kind of looking for a logic like this.
     
  5. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    "networkView.owner.ToString();"
    That was what i was looking for as Network.player.ToString(); was returning my own number.
    Thanks ^_^
     
  6. Scorr

    Scorr

    Joined:
    Jul 2, 2013
    Posts:
    73
    Just so people don't get confused, this thread (from 2007!) is about Unity's old networking system (RakNet) and doesn't apply to UNET.