According to the "High Level API" documentation, unless I misread something, a Host "just" runs both as a Server and as a Client, so much that it would require no special support, i.e. it should run out of the box.
When the Server and Client are in separate processes, both work correctly. When a host acts as both the server and client, this is not the case.
When OnServerReady
gets called on the Host, the NetworkConnection conn
parameter has .hostId==-1
, and if I try to call on NetworkServer.AddPlayerForConnection
I'll get a warning and an error.
ClientScene::InternalAddPlayer: playerControllerId higher than expected: 0
UnityEngine.Networking.NetworkServer:AddPlayerForConnection(NetworkConnection, GameObject, Int16)
[...]
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.ClientScene.InternalAddPlayer (UnityEngine.Networking.NetworkIdentity view, Int16 playerControllerId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:90)
[...]
If I refrain from calling AddPlayerForConnection
, then I'll encounter a warning (and no result) as soon as the Host calls a Command
:
Trying to send command for non-local player.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
[...]
In fact, isLocalPlayer
is false
.
Am I doing something wrong, is the documentation wrong, or is this a bug?
As requested, some piece of code:
public override void OnServerReady (NetworkConnection conn)
{
base.OnServerReady(conn);
Transform spawnPoint = FindObjectOfType<SpawnPoints>().getNextSpawnLocation();
GameObject controllerGO = instancedElements.SpawnObjectFree(playerPrefab,spawnPoint); // this is basically an Instantiate
GameObject playerGO = instancedElements.SpawnObjectFree(playerCreaturePrefab,spawnPoint);
[...]
NetworkServer.AddPlayerForConnection(conn,controllerGO,0);
NetworkServer.Spawn(playerGO);
[...]
}
(I've trimmed it down to remove the non-relevant parts)
OnServerReady
, the client does nothing special on setup, let me know if you need me to dig something else… \$\endgroup\$