From 31b3035de137a017c2f9af12bbc61c23d75ecea5 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Thu, 2 Aug 2018 08:03:02 -0500 Subject: [PATCH] Set client owner before we send spawn messages --- .../Runtime/NetworkServer.cs | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Unity-Technologies-networking/Runtime/NetworkServer.cs b/Unity-Technologies-networking/Runtime/NetworkServer.cs index e30a927bb..0aedc284f 100644 --- a/Unity-Technologies-networking/Runtime/NetworkServer.cs +++ b/Unity-Technologies-networking/Runtime/NetworkServer.cs @@ -740,10 +740,6 @@ static internal bool InternalAddPlayerForConnection(NetworkConnection conn, Game if (LogFilter.logDebug) { Debug.Log("Adding new playerGameObject object netId: " + playerGameObject.GetComponent().netId + " asset ID " + playerGameObject.GetComponent().assetId); } FinishPlayerForConnection(conn, playerNetworkIdentity, playerGameObject); - if (playerNetworkIdentity.localPlayerAuthority) - { - playerNetworkIdentity.SetClientOwner(conn); - } return true; } @@ -782,12 +778,17 @@ static bool SetupLocalPlayerForConnection(NetworkConnection conn, NetworkIdentit // so dont spawn it again. uv.OnStartServer(true); } + // paul: The owner must be set before we rebuild observers + // during rebuild observers spawn messages might be sent + //and if we don't have the owner, we won't receive owner data + uv.SetClientOwner(conn); + uv.RebuildObservers(true); SendSpawnMessage(uv, null); // Set up local player instance on the client instance and update local object map localConnection.localClient.AddLocalPlayer(newPlayerController); - uv.SetClientOwner(conn); + // Trigger OnAuthority uv.ForceAuthority(true); @@ -805,7 +806,7 @@ static void FinishPlayerForConnection(NetworkConnection conn, NetworkIdentity uv { // it is allowed to provide an already spawned object as the new player object. // so dont spawn it again. - Spawn(playerGameObject); + Spawn(playerGameObject, conn); } OwnerMessage owner = new OwnerMessage(); @@ -855,10 +856,6 @@ static internal bool InternalReplacePlayerForConnection(NetworkConnection conn, if (LogFilter.logDebug) { Debug.Log("Replacing playerGameObject object netId: " + playerGameObject.GetComponent().netId + " asset ID " + playerGameObject.GetComponent().assetId); } FinishPlayerForConnection(conn, playerNetworkIdentity, playerGameObject); - if (playerNetworkIdentity.localPlayerAuthority) - { - playerNetworkIdentity.SetClientOwner(conn); - } return true; } @@ -1063,7 +1060,7 @@ static void OnCommandMessage(NetworkMessage netMsg) uv.HandleCommand(message.cmdHash, new NetworkReader(message.payload)); } - static internal void SpawnObject(GameObject obj) + static internal void SpawnObject(GameObject obj, NetworkConnection conn = null) { if (!NetworkServer.active) { @@ -1083,6 +1080,13 @@ static internal void SpawnObject(GameObject obj) if (LogFilter.logDebug) { Debug.Log("SpawnObject instance ID " + objNetworkIdentity.netId + " asset ID " + objNetworkIdentity.assetId); } + // paul: we must set the owner before rebuilding observers or we won't be sending + // owner data to the owner + if (objNetworkIdentity.localPlayerAuthority && conn != null) + { + objNetworkIdentity.SetClientOwner(conn); + } + objNetworkIdentity.RebuildObservers(true); //SendSpawnMessage(objNetworkIdentity, null); } @@ -1308,11 +1312,11 @@ static public void ClearLocalObjects() objects.Clear(); } - static public void Spawn(GameObject obj) + static public void Spawn(GameObject obj, NetworkConnection conn = null) { if (VerifyCanSpawn(obj)) { - SpawnObject(obj); + SpawnObject(obj, conn); } } @@ -1362,7 +1366,7 @@ static public bool SpawnWithClientAuthority(GameObject obj, NetworkConnection co return false; } - Spawn(obj); + Spawn(obj, conn); var uv = obj.GetComponent(); if (uv == null || !uv.isServer) @@ -1376,7 +1380,7 @@ static public bool SpawnWithClientAuthority(GameObject obj, NetworkConnection co static public bool SpawnWithClientAuthority(GameObject obj, NetworkHash128 assetId, NetworkConnection conn) { - Spawn(obj, assetId); + Spawn(obj, assetId, conn); var uv = obj.GetComponent(); if (uv == null || !uv.isServer) @@ -1388,7 +1392,7 @@ static public bool SpawnWithClientAuthority(GameObject obj, NetworkHash128 asset return uv.AssignClientAuthority(conn); } - static public void Spawn(GameObject obj, NetworkHash128 assetId) + static public void Spawn(GameObject obj, NetworkHash128 assetId, NetworkConnection conn = null) { if (VerifyCanSpawn(obj)) { @@ -1397,7 +1401,7 @@ static public void Spawn(GameObject obj, NetworkHash128 assetId) { id.SetDynamicAssetId(assetId); } - SpawnObject(obj); + SpawnObject(obj, conn); } }