Skip to content

Commit 3d4a803

Browse files
update
Temporary fix for missed sync.
1 parent ea80eb0 commit 3d4a803

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
348348
{
349349
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
350350
}
351+
352+
if (!IsServer)
353+
{
354+
SpawnManager?.CheckGhostsPendingNetworkObjectId();
355+
}
351356
#endif
352357

353358
UpdateTopology();

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,13 +3854,27 @@ private void InitGhost()
38543854
}
38553855
}
38563856

3857-
private void RegisterGhostBridge()
3857+
internal bool IsGhostNetworkObjectIdValid()
3858+
{
3859+
if (NetworkObjectBridge == null)
3860+
{
3861+
return false;
3862+
}
3863+
// TODO-UNIFIED: Sometimes the GhostField can be latent. Need a way to know the GhostField has been set.
3864+
return NetworkObjectBridge.NetworkObjectId.Value < 1000000;
3865+
}
3866+
3867+
internal void RegisterGhostBridge()
38583868
{
38593869
if (NetworkManager.LogLevel == LogLevel.Developer)
38603870
{
38613871
Debug.Log($"[{nameof(NetworkObject)}][{nameof(NetworkObjectId)}] NetworkObjectBridge notified instance exists with assigned ID of: {NetworkObjectBridge.NetworkObjectId.Value}");
38623872
}
3863-
NetworkManager.SpawnManager.RegisterGhostPendingSpawn(this, NetworkObjectBridge.NetworkObjectId.Value);
3873+
3874+
if (!NetworkManager.IsServer)
3875+
{
3876+
NetworkManager.SpawnManager.RegisterGhostPendingSpawn(this, NetworkObjectBridge.NetworkObjectId.Value);
3877+
}
38643878
}
38653879

38663880
private void OnNetworkObjectIdChanged(ulong networkObjectId)

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,59 @@ public class NetworkSpawnManager
3434

3535
internal readonly Dictionary<ulong, NetworkObject> GhostsPendingSpawn = new Dictionary<ulong, NetworkObject>();
3636

37-
public void RegisterGhostPendingSpawn(NetworkObject networkObject, ulong networkObjectId)
37+
internal readonly List<NetworkObject> GhostsPendingNetworkObjectId = new List<NetworkObject>();
38+
39+
internal void CheckGhostsPendingNetworkObjectId()
40+
{
41+
if (GhostsPendingNetworkObjectId.Count == 0)
42+
{
43+
return;
44+
}
45+
46+
for(int i = GhostsPendingNetworkObjectId.Count - 1; i >= 0; i--)
47+
{
48+
var networkObject = GhostsPendingNetworkObjectId[i];
49+
if (networkObject.IsGhostNetworkObjectIdValid())
50+
{
51+
GhostsPendingNetworkObjectId.Remove(networkObject);
52+
if (NetworkManager.LogLevel == LogLevel.Developer)
53+
{
54+
Debug.Log($"[{nameof(RegisterGhostPendingSpawn)}] {networkObject.name}'s Ghost {nameof(NetworkObject.NetworkObjectId)} is valid. Re-registering.");
55+
}
56+
networkObject.RegisterGhostBridge();
57+
}
58+
}
59+
}
60+
61+
internal void RegisterGhostPendingSpawn(NetworkObject networkObject, ulong networkObjectId)
3862
{
63+
if (!networkObject.IsGhostNetworkObjectIdValid())
64+
{
65+
GhostsPendingNetworkObjectId.Add(networkObject);
66+
if (NetworkManager.LogLevel == LogLevel.Developer)
67+
{
68+
Debug.Log($"[{nameof(RegisterGhostPendingSpawn)}] {networkObject.name}'s Ghost {nameof(NetworkObject.NetworkObjectId)} ({networkObjectId}) seems invalid. Adding to the pending NetworkObjectId list.");
69+
}
70+
return;
71+
}
3972
if (NetworkManager.LogLevel == LogLevel.Developer)
4073
{
4174
Debug.Log($"[{nameof(RegisterGhostPendingSpawn)}] Registering {networkObject.name} with a {nameof(NetworkObject.NetworkObjectId)} of {networkObjectId}.");
4275
}
4376
if(GhostsPendingSpawn.TryAdd(networkObjectId, networkObject))
4477
{
4578
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
46-
// For now, move any pending object into the DDOL.
79+
// For now, move any pending object into the DDOL.
4780
UnityEngine.Object.DontDestroyOnLoad(networkObject.gameObject);
4881
}
4982

5083
NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnGhostSpawned, networkObjectId);
5184
if (GhostsArePendingSynchronization && GhostsPendingSynchronization.ContainsKey(networkObjectId))
5285
{
86+
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
87+
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
88+
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
89+
5390
// When the object is spawned, it will invoke GetGhostNetworkObjectForSpawn below which removes the entry from GhostsPendingSpawn
5491
ProcessGhostPendingSynchronization(networkObjectId);
5592
}
@@ -64,6 +101,9 @@ internal NetworkObject GetGhostNetworkObjectForSpawn(ulong networkObjectId)
64101
}
65102
var networkObject = GhostsPendingSpawn[networkObjectId];
66103
GhostsPendingSpawn.Remove(networkObjectId);
104+
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
105+
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
106+
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
67107
return networkObject;
68108
}
69109

@@ -100,10 +140,6 @@ internal void ProcessGhostPendingSynchronization(ulong networkObjectId, bool rem
100140
//{
101141
// networkObject.InternalInSceneNetworkObjectsSpawned();
102142
//}
103-
104-
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
105-
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
106-
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
107143
if (removeUponSpawn)
108144
{
109145
GhostsArePendingSynchronization = GhostsPendingSynchronization.Count > 0;
@@ -2020,6 +2056,12 @@ internal NetworkSpawnManager(NetworkManager networkManager)
20202056

20212057
internal void Shutdown()
20222058
{
2059+
#if UNIFIED_NETCODE
2060+
GhostsPendingNetworkObjectId.Clear();
2061+
GhostsPendingNetworkObjectId.Clear();
2062+
GhostsPendingSpawn.Clear();
2063+
GhostsPendingSynchronization.Clear();
2064+
#endif
20232065
NetworkObjectsToSynchronizeSceneChanges.Clear();
20242066
CleanUpDisposedObjects.Clear();
20252067
}

0 commit comments

Comments
 (0)