Skip to content

Commit

Permalink
feat: Spawn objects in clients in same order as server (#247)
Browse files Browse the repository at this point in the history
If I spawn 10 objects in the server,  when a client connect
they should be spawned in the client in the same order.

Co-authored-by: Paul Pacheco <paul.pacheco@aa.com>
  • Loading branch information
paulpach and paulpach committed Jul 5, 2020
1 parent 462915c commit b786646
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,14 @@ internal bool ValidateSceneObject(NetworkIdentity identity)
return identity.sceneId != 0;
}

private class NetworkIdentityComparer : IComparer<NetworkIdentity>
{
public int Compare(NetworkIdentity x, NetworkIdentity y)
{
return x.NetId.CompareTo(y.NetId);
}
}

/// <summary>
/// This causes NetworkIdentity objects in a scene to be spawned on a server.
/// <para>NetworkIdentity objects in a scene are disabled by default. Calling SpawnObjects() causes these scene objects to be enabled and spawned. It is like calling NetworkServer.Spawn() for each of them.</para>
Expand All @@ -1091,6 +1099,8 @@ public bool SpawnObjects()
return false;

NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll<NetworkIdentity>();
Array.Sort(identities, new NetworkIdentityComparer());

foreach (NetworkIdentity identity in identities)
{
if (ValidateSceneObject(identity))
Expand Down

0 comments on commit b786646

Please sign in to comment.