1
\$\begingroup\$

In Unity/UNet: How do you properly spawn a NetworkPlayer? Right now, I'm doing it like this from inside a NetworkManager derived class:

   public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
        NetworkPlayer newPlayer = Instantiate<NetworkPlayer>(m_NetworkPlayerPrefab);
        DontDestroyOnLoad(newPlayer);
        NetworkServer.AddPlayerForConnection(conn, newPlayer.gameObject, playerControllerId);
   }

This code snippet works pretty well and both clients can communicate with each other. However, there are a few little issues that arise only on the host:

  1. In Unity's hierarchy-view on the host, there are only two NetworkPlayer instances. Shouldn't there be four NetworkPlayer instances on the host? Two client instances and two server instances? If so, do you have any ideas what could cause the missing NetworkPlayer instances?
  2. The two NetworkPlayer instances have both, their isClient and isServer flags set to true. But only one of the has it's isLocalPlayer flag set. Now I wonder if this behavior is as intended? And if so, how do you distinguish between the client and the server instance of a NetworkPlayer?
  3. Two player behavior: If the remote client sends a [Command] that changes a [SyncVar] on the server, then on the host, the [SyncVar]-hook is called only on the NetworkPlayer instance that represents the remote NetworkPlayer. The [SyncVar]-hook is not called on the host's "isLocalPlayer-NetworkPlayer" instance. Shouldn't the [SyncVar]-hook be called on both NetworkPlayer instances?

Any advise is welcome. Thank you!

\$\endgroup\$
0
\$\begingroup\$

In Unity's hierarchy-view on the host, there are only two NetworkPlayer instances. Shouldn't there be four NetworkPlayer instances on the host? Two client instances and two server instances? If so, do you have any ideas what could cause the missing NetworkPlayer instances?

No, two is the correct number. The only difference between the Host and the Client when it comes to things, is that the Clients defer to the Host about the position of the GameObjects and their state. If the Host moves an object 1 unit to the left, the Clients will all move their version to match.

The two NetworkPlayer instances have both, their isClient and isServer flags set to true. But only one of the has it's isLocalPlayer flag set. Now I wonder if this behavior is as intended? And if so, how do you distinguish between the client and the server instance of a NetworkPlayer?

NetworkPlayer doesn't have those attributes, Network has them. Network.isClient will be true if the Network instance is a Client, likewise Network.isServer will be true if the Network instance is the Host.

isLocalPlayer will be true if the NetworkPlayer instance is the instance representing the local machine in the game. Only one NetworkPlayer instance will have an isLocalPlayer value of true in a running instance of your game. Think of this variable as the "me" flag: think "this object is me".

Two player behavior: If the remote client sends a [Command] that changes a [SyncVar] on the server, then on the host, the [SyncVar]-hook is called only on the NetworkPlayer instance that represents the remote NetworkPlayer. The [SyncVar]-hook is not called on the host's "isLocalPlayer-NetworkPlayer" instance. Shouldn't the [SyncVar]-hook be called on both NetworkPlayer instances?

No. As before, the isLocalPlayer=true NetworkPlayer instance is the avatar/character controlled by the human on the Host machine. The [Command] is being called by the Client to ask the Host to update the state of the Host's copy of the Client's NetworkPlayer instance. The Host will update the value on it's copy of the GameObject representing the remote human player, then forward the change to the other Clients in the game.

--

One thing to consider here is the directionality of the various pieces of the networking code:

[Command]s are sent from Client to Host

[ClientRPC]s are sent from Host to Clients

[SyncVar]s are sent from Host to Clients

See the documentation HERE for more information.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.