Search Unity

ReAssigning/Reconnecting Client and controls to PlayerObject on server - ReplacePlayerForConnection

Discussion in 'Multiplayer' started by ZackNewworldcoders, Jan 27, 2017.

  1. ZackNewworldcoders

    ZackNewworldcoders

    Joined:
    Jul 19, 2016
    Posts:
    20
    Hi there! Thank you in advance for taking a look or any help.

    I have a project where a user can host a game and be a client/host on the current machine,

    I have the project running on a laptop for a second client for testing.

    I have overriden the NetworkManager Methods : OnServerDisconnect and OnServerRemovePlayer so that the player component script has a isOnline bool set to false, and the players game object is not destroyed.

    Upon Connecting the client to the server the Server cycles through current player game objects by tag, and then compares a unique ID in order to confirm the connecting client either has an existing object on the server... If no object is found a NetworkServer.AddPlayerForConnection is called and adds a player object.

    That all WORKS EXCELLENT.

    However... Upon RE-Connecting... if the server DOES find a MATCHING Unique ID for the player... the server calls a NetworkServer.ReplacePlayerForConnection() with the clients incomming NetworkConnection and playercontrollerid, and assigns the "FoundPlayerObject"

    What seems to be happening is " NetworkServer.ReplacePlayerForConnection() " being called on the server seems to assign the server/host input to the connecting clients object.... causing server machine input to be ran on the client object...

    Any Ideas?


    TL : DR -- When a Client Reconnects, Server Determines if Existing Player Object is available and try's to assign connecting clients input to the found player object --- Actual Result : Server Input is assigned to Found Client Object :




    Some Code...
    Code (csharp):
    1.  
    2. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
    3.     {
    4.        
    5.         string connectingclientsid = "";
    6.         string connectingclientsname = "";
    7.         if (extraMessageReader != null)
    8.         {
    9.            
    10.             var id = extraMessageReader.ReadMessage<StringMessage>();
    11.  
    12.             string[] splitstring = ((string)id.value).Split(MessageSeperator);
    13.  
    14.            
    15.  
    16.             Debug.Log("Connecting Clients ID:" + splitstring[0] + " NickName : " + splitstring[1]);
    17.             connectingclientsid = splitstring[0];
    18.             connectingclientsname = splitstring[1];
    19.         }
    20.  
    21.  
    22.         GameObject[] savedRootPlayers = GameObject.FindGameObjectsWithTag("RootPlayer");
    23.         bool matchfound = false;
    24.         bool IDAlreadyInUse = false;
    25.         if (connectingclientsid != "")
    26.         {
    27.             for (int x = 0; x < savedRootPlayers.Length; x++)
    28.             {
    29.                 string savedPlayerID = savedRootPlayers[x].GetComponent<Player>().playersID;
    30.                          
    31.                 if (savedPlayerID == connectingclientsid)
    32.                 {
    33.                     // User is not online.... Connect Client to Player Object....
    34.                     if (savedRootPlayers[x].GetComponent<Player>().isOnline == false)
    35.                     {
    36.                         savedRootPlayers[x].transform.GetChild(0).gameObject.SetActive(true);
    37.                         savedRootPlayers[x].transform.GetChild(0).gameObject.GetComponent<FPSMouseLook>().enabled = true;
    38.                         savedRootPlayers[x].GetComponent<FPSMouseLook>().enabled = true;
    39.                         savedRootPlayers[x].GetComponent<FPScontroller>().enabled = true;
    40.                         savedRootPlayers[x].GetComponent<FPSinput>().enabled = true;
    41.                         savedRootPlayers[x].GetComponent<LadderPlayer>().enabled = true;
    42.                         savedRootPlayers[x].GetComponent<WeaponPickUp>().enabled = true;
    43.                         savedRootPlayers[x].GetComponent<NWC_FPSInput>().enabled = true;
    44.  
    45.                             WARDebug.Log("ReconnectedPlayerObject: Trying To Re-Assign PlayerObject For:" + connectingclientsid);
    46.                             savedRootPlayers[x].GetComponent<Player>().isOnline = true;
    47.                            
    48.                            // Think My Problems Here:
    49. // I think it has to do with player controller, i have tried the conn.playercontroller and i have tried straight 0
    50. // And i have tried with and without assigning client authority for kicks, but dont belive that has anything to do with it
    51.                             NetworkServer.ReplacePlayerForConnection(conn, savedRootPlayers[x], playerControllerId);
    52.  
    53.                         NetworkServer.SetClientReady(conn);
    54.                        
    55.                         WARDebug.Log("Found Player Object Match By ID : " + connectingclientsid + " Assigning Connection To Player Object : " + savedRootPlayers[x]);
    56.                         GameManager.RegisterPlayer(savedRootPlayers[x].GetComponent<NetworkIdentity>().netId.ToString(), savedRootPlayers[x].GetComponent<Player>(), savedRootPlayers[x].transform.name);
    57.                        
    58.                         matchfound = true;
    59.                         break;
    60.  
    61.                     }
    62.                     else
    63.                     {
    64.                         IDAlreadyInUse = true;
    65.                     }
    66.                 }
    67.             }
    68.         }
    69.  
    70.         if (!matchfound)
    71.         {
    72.             GameObject player = Instantiate<GameObject>(GameManager.GetGameObjectByString("NetworkPlayerPrefab"), Vector3.zero, Quaternion.identity);
    73.             player.transform.name = connectingclientsid;
    74.            
    75.             player.GetComponent<Player>().playersID = connectingclientsid;
    76.             GameManager.RegisterPlayer(player.GetComponent<NetworkIdentity>().netId.ToString(), player.GetComponent<Player>(), player.transform.name);
    77.  
    78.             WARDebug.Log("Created New Player Object For:" + connectingclientsid);
    79.             player.GetComponent<Player>().isOnline = true;
    80.             NetworkServer.AddPlayerForConnection(conn, player, 0);
    81.         }
    82.         // Commented out, as we are manually handelling the operations....
    83.         //base.OnServerAddPlayer(conn, playerControllerId);
    84.     }
    85.  
    86.  
    87.  
    88.  
    89.  
    90.