-
Notifications
You must be signed in to change notification settings - Fork 450
refactor!: SpawnManager is no longer static #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -508,7 +508,7 @@ internal static void NetworkBehaviourUpdate() | |||||||
for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++) | ||||||||
{ | ||||||||
var client = NetworkManager.Singleton.ConnectedClientsList[i]; | ||||||||
var spawnedObjs = NetworkSpawnManager.SpawnedObjectsList; | ||||||||
var spawnedObjs = NetworkManager.Singleton.SpawnManager.SpawnedObjectsList; | ||||||||
s_Touched.UnionWith(spawnedObjs); | ||||||||
foreach (var sobj in spawnedObjs) | ||||||||
{ | ||||||||
|
@@ -532,7 +532,7 @@ internal static void NetworkBehaviourUpdate() | |||||||
else | ||||||||
{ | ||||||||
// when client updates the sever, it tells it about all its objects | ||||||||
foreach (var sobj in NetworkSpawnManager.SpawnedObjectsList) | ||||||||
foreach (var sobj in NetworkManager.Singleton.SpawnManager.SpawnedObjectsList) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto as above, actually making the induction variable longer doesn't to me improve readability |
||||||||
{ | ||||||||
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++) | ||||||||
{ | ||||||||
|
@@ -541,7 +541,7 @@ internal static void NetworkBehaviourUpdate() | |||||||
} | ||||||||
|
||||||||
// Now, reset all the no-longer-dirty variables | ||||||||
foreach (var sobj in NetworkSpawnManager.SpawnedObjectsList) | ||||||||
foreach (var sobj in NetworkManager.Singleton.SpawnManager.SpawnedObjectsList) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
{ | ||||||||
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++) | ||||||||
{ | ||||||||
|
@@ -1006,6 +1006,6 @@ internal static void SetNetworkVariableData(List<INetworkVariable> networkVariab | |||||||
/// </summary> | ||||||||
/// <param name="networkId"></param> | ||||||||
/// <returns></returns> | ||||||||
protected NetworkObject GetNetworkObject(ulong networkId) => NetworkSpawnManager.SpawnedObjects.ContainsKey(networkId) ? NetworkSpawnManager.SpawnedObjects[networkId] : null; | ||||||||
protected NetworkObject GetNetworkObject(ulong networkId) => NetworkManager.Singleton.SpawnManager.SpawnedObjects.ContainsKey(networkId) ? NetworkManager.Singleton.SpawnManager.SpawnedObjects[networkId] : null; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -89,6 +89,11 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem, IProfilableTr | |||||
/// </summary> | ||||||
public static NetworkManager Singleton { get; private set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Gets the SpawnManager for this NetworkManager | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// </summary> | ||||||
public NetworkSpawnManager SpawnManager { get; private set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Gets the networkId of the server | ||||||
/// </summary> | ||||||
|
@@ -313,10 +318,9 @@ private void Init(bool server) | |||||
ConnectedClients.Clear(); | ||||||
ConnectedClientsList.Clear(); | ||||||
|
||||||
NetworkSpawnManager.SpawnedObjects.Clear(); | ||||||
NetworkSpawnManager.SpawnedObjectsList.Clear(); | ||||||
NetworkSpawnManager.ReleasedNetworkObjectIds.Clear(); | ||||||
NetworkSpawnManager.PendingSoftSyncObjects.Clear(); | ||||||
// Create spawn manager instance | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
isn't it obvious already? 😛 |
||||||
SpawnManager = new NetworkSpawnManager(this); | ||||||
|
||||||
NetworkSceneManager.RegisteredSceneNames.Clear(); | ||||||
NetworkSceneManager.SceneIndexToString.Clear(); | ||||||
NetworkSceneManager.SceneNameToIndex.Clear(); | ||||||
|
@@ -441,7 +445,7 @@ public SocketTasks StartServer() | |||||
IsClient = false; | ||||||
IsListening = true; | ||||||
|
||||||
NetworkSpawnManager.ServerSpawnSceneObjectsOnStartSweep(); | ||||||
SpawnManager.ServerSpawnSceneObjectsOnStartSweep(); | ||||||
|
||||||
OnServerStarted?.Invoke(); | ||||||
|
||||||
|
@@ -617,7 +621,7 @@ public SocketTasks StartHost() | |||||
HandleApproval(ServerClientId, NetworkConfig.CreatePlayerPrefab, null, true, null, null); | ||||||
} | ||||||
|
||||||
NetworkSpawnManager.ServerSpawnSceneObjectsOnStartSweep(); | ||||||
SpawnManager.ServerSpawnSceneObjectsOnStartSweep(); | ||||||
|
||||||
OnServerStarted?.Invoke(); | ||||||
|
||||||
|
@@ -691,8 +695,14 @@ public void Shutdown() | |||||
IsServer = false; | ||||||
IsClient = false; | ||||||
NetworkConfig.NetworkTransport.OnTransportEvent -= HandleRawTransportPoll; | ||||||
NetworkSpawnManager.DestroyNonSceneObjects(); | ||||||
NetworkSpawnManager.ServerResetShudownStateForSceneObjects(); | ||||||
|
||||||
if (SpawnManager != null) | ||||||
{ | ||||||
SpawnManager.DestroyNonSceneObjects(); | ||||||
SpawnManager.ServerResetShudownStateForSceneObjects(); | ||||||
|
||||||
SpawnManager = null; | ||||||
} | ||||||
|
||||||
//The Transport is set during Init time, thus it is possible for the Transport to be null | ||||||
NetworkConfig?.NetworkTransport?.Shutdown(); | ||||||
|
@@ -1213,7 +1223,7 @@ private static void ReceiveCallback(NetworkBuffer messageBuffer, RpcQueueContain | |||||
/// </summary> | ||||||
/// <param name="queueItem">frame queue item to invoke</param> | ||||||
#pragma warning disable 618 | ||||||
internal static void InvokeRpc(RpcFrameQueueItem queueItem) | ||||||
internal void InvokeRpc(RpcFrameQueueItem queueItem) | ||||||
{ | ||||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR | ||||||
s_InvokeRpc.Begin(); | ||||||
|
@@ -1225,12 +1235,12 @@ internal static void InvokeRpc(RpcFrameQueueItem queueItem) | |||||
|
||||||
if (__ntable.ContainsKey(networkMethodId)) | ||||||
{ | ||||||
if (!NetworkSpawnManager.SpawnedObjects.ContainsKey(networkObjectId)) | ||||||
if (!SpawnManager.SpawnedObjects.ContainsKey(networkObjectId)) | ||||||
{ | ||||||
return; | ||||||
} | ||||||
|
||||||
var networkObject = NetworkSpawnManager.SpawnedObjects[networkObjectId]; | ||||||
var networkObject = SpawnManager.SpawnedObjects[networkObjectId]; | ||||||
|
||||||
var networkBehaviour = networkObject.GetNetworkBehaviourAtOrderIndex(networkBehaviourId); | ||||||
if (networkBehaviour == null) | ||||||
|
@@ -1344,10 +1354,10 @@ internal void OnClientDisconnectFromServer(ulong clientId) | |||||
{ | ||||||
if (ConnectedClients[clientId].PlayerObject != null) | ||||||
{ | ||||||
if (NetworkSpawnManager.CustomDestroyHandlers.ContainsKey(ConnectedClients[clientId].PlayerObject.PrefabHash)) | ||||||
if (SpawnManager.CustomDestroyHandlers.ContainsKey(ConnectedClients[clientId].PlayerObject.PrefabHash)) | ||||||
{ | ||||||
NetworkSpawnManager.CustomDestroyHandlers[ConnectedClients[clientId].PlayerObject.PrefabHash](ConnectedClients[clientId].PlayerObject); | ||||||
NetworkSpawnManager.OnDestroyObject(ConnectedClients[clientId].PlayerObject.NetworkObjectId, false); | ||||||
SpawnManager.CustomDestroyHandlers[ConnectedClients[clientId].PlayerObject.PrefabHash](ConnectedClients[clientId].PlayerObject); | ||||||
SpawnManager.OnDestroyObject(ConnectedClients[clientId].PlayerObject.NetworkObjectId, false); | ||||||
} | ||||||
else | ||||||
{ | ||||||
|
@@ -1361,10 +1371,10 @@ internal void OnClientDisconnectFromServer(ulong clientId) | |||||
{ | ||||||
if (!ConnectedClients[clientId].OwnedObjects[i].DontDestroyWithOwner) | ||||||
{ | ||||||
if (NetworkSpawnManager.CustomDestroyHandlers.ContainsKey(ConnectedClients[clientId].OwnedObjects[i].PrefabHash)) | ||||||
if (SpawnManager.CustomDestroyHandlers.ContainsKey(ConnectedClients[clientId].OwnedObjects[i].PrefabHash)) | ||||||
{ | ||||||
NetworkSpawnManager.CustomDestroyHandlers[ConnectedClients[clientId].OwnedObjects[i].PrefabHash](ConnectedClients[clientId].OwnedObjects[i]); | ||||||
NetworkSpawnManager.OnDestroyObject(ConnectedClients[clientId].OwnedObjects[i].NetworkObjectId, false); | ||||||
SpawnManager.CustomDestroyHandlers[ConnectedClients[clientId].OwnedObjects[i].PrefabHash](ConnectedClients[clientId].OwnedObjects[i]); | ||||||
SpawnManager.OnDestroyObject(ConnectedClients[clientId].OwnedObjects[i].NetworkObjectId, false); | ||||||
} | ||||||
else | ||||||
{ | ||||||
|
@@ -1380,7 +1390,7 @@ internal void OnClientDisconnectFromServer(ulong clientId) | |||||
|
||||||
// TODO: Could(should?) be replaced with more memory per client, by storing the visiblity | ||||||
|
||||||
foreach (var sobj in NetworkSpawnManager.SpawnedObjectsList) | ||||||
foreach (var sobj in SpawnManager.SpawnedObjectsList) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
sobj.Observers.Remove(clientId); | ||||||
} | ||||||
|
@@ -1446,15 +1456,15 @@ internal void HandleApproval(ulong ownerClientId, bool createPlayerObject, ulong | |||||
|
||||||
if (createPlayerObject) | ||||||
{ | ||||||
var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(false, 0, playerPrefabHash ?? NetworkConfig.PlayerPrefabHash.Value, ownerClientId, null, position, rotation); | ||||||
NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, NetworkSpawnManager.GetNetworkObjectId(), false, true, ownerClientId, null, false, 0, false, false); | ||||||
var networkObject = SpawnManager.CreateLocalNetworkObject(false, 0, playerPrefabHash ?? NetworkConfig.PlayerPrefabHash.Value, ownerClientId, null, position, rotation); | ||||||
SpawnManager.SpawnNetworkObjectLocally(networkObject, SpawnManager.GetNetworkObjectId(), false, true, ownerClientId, null, false, 0, false, false); | ||||||
|
||||||
ConnectedClients[ownerClientId].PlayerObject = networkObject; | ||||||
} | ||||||
|
||||||
m_ObservedObjects.Clear(); | ||||||
|
||||||
foreach (var sobj in NetworkSpawnManager.SpawnedObjectsList) | ||||||
foreach (var sobj in SpawnManager.SpawnedObjectsList) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
if (ownerClientId == ServerClientId || sobj.CheckObjectVisibility == null || sobj.CheckObjectVisibility(ownerClientId)) | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -225,7 +225,7 @@ public void NetworkShow(ulong clientId, Stream payload = null) | |||||
// Send spawn call | ||||||
Observers.Add(clientId); | ||||||
|
||||||
NetworkSpawnManager.SendSpawnCallForObject(clientId, this, payload); | ||||||
NetworkManager.Singleton.SpawnManager.SendSpawnCallForObject(clientId, this, payload); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -265,7 +265,7 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI | |||||
// Send spawn call | ||||||
networkObjects[i].Observers.Add(clientId); | ||||||
|
||||||
NetworkSpawnManager.WriteSpawnCallForObject(buffer, clientId, networkObjects[i], payload); | ||||||
NetworkManager.Singleton.SpawnManager.WriteSpawnCallForObject(buffer, clientId, networkObjects[i], payload); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
InternalMessageSender.Send(clientId, NetworkConstants.ADD_OBJECTS, NetworkChannel.Internal, buffer); | ||||||
|
@@ -361,9 +361,9 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI | |||||
|
||||||
private void OnDestroy() | ||||||
{ | ||||||
if (NetworkManager.Singleton != null && NetworkSpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId)) | ||||||
if (NetworkManager.Singleton != null && NetworkManager.Singleton.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
NetworkSpawnManager.OnDestroyObject(NetworkObjectId, false); | ||||||
NetworkManager.Singleton.SpawnManager.OnDestroyObject(NetworkObjectId, false); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -385,13 +385,13 @@ private void SpawnInternal(Stream spawnPayload, bool destroyWithScene, ulong? ow | |||||
spawnPayload.Position = 0; | ||||||
} | ||||||
|
||||||
NetworkSpawnManager.SpawnNetworkObjectLocally(this, NetworkSpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene); | ||||||
NetworkManager.Singleton.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.Singleton.SpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++) | ||||||
{ | ||||||
if (Observers.Contains(NetworkManager.Singleton.ConnectedClientsList[i].ClientId)) | ||||||
{ | ||||||
NetworkSpawnManager.SendSpawnCallForObject(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, this, spawnPayload); | ||||||
NetworkManager.Singleton.SpawnManager.SendSpawnCallForObject(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, this, spawnPayload); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -433,7 +433,7 @@ public void SpawnAsPlayerObject(ulong clientId, Stream spawnPayload = null, bool | |||||
/// </summary> | ||||||
public void Despawn(bool destroy = false) | ||||||
{ | ||||||
NetworkSpawnManager.DespawnObject(this, destroy); | ||||||
NetworkManager.Singleton.SpawnManager.DespawnObject(this, destroy); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
|
||||||
|
@@ -442,7 +442,7 @@ public void Despawn(bool destroy = false) | |||||
/// </summary> | ||||||
public void RemoveOwnership() | ||||||
{ | ||||||
NetworkSpawnManager.RemoveOwnership(this); | ||||||
NetworkManager.Singleton.SpawnManager.RemoveOwnership(this); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -451,7 +451,7 @@ public void RemoveOwnership() | |||||
/// <param name="newOwnerClientId">The new owner clientId</param> | ||||||
public void ChangeOwnership(ulong newOwnerClientId) | ||||||
{ | ||||||
NetworkSpawnManager.ChangeOwnership(this, newOwnerClientId); | ||||||
NetworkManager.Singleton.SpawnManager.ChangeOwnership(this, newOwnerClientId); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
internal void InvokeBehaviourOnLostOwnership() | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree and disagree, but mostly disagree. The name change is maybe valid (though having small insignificant names for insignificant variables and vice versa is a style choice I actually prefer), but there's also the "when in Rome" principal where we want to make the PR as focused on the actual change at hand, not mix in some minor unrelated style edits to keep the signal / noise ratio high.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you'd consider this change in a separate PR.
we're changing it from:
to
already.
my only suggestion here is to kill
.Singleton
part as wellbecause there is already
NetworkBehaviour.NetworkManager
property available, so there is no need to accessNetworkManager
overNetworkManager.Singleton
instead ofNetworkBehaviour.NetworkManager
.I do not think it should go under a separate PR — we are making a change there already, let's make it even better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will come in my next PR!
That's where I will convert the whole NetworkObject & Behaviour.