From 8c6d9bf1da67f8dcda4d24d4a3018c85fbffe8e7 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 14:19:52 -0400 Subject: [PATCH 01/12] replacing custom pool implementation with native one --- .../Infrastructure/NetworkObjectPool.cs | 52 +++++++------------ 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 4c18daa1a..84339deba 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -4,6 +4,7 @@ using Unity.Netcode; using UnityEngine; using UnityEngine.Assertions; +using UnityEngine.Pool; namespace Unity.BossRoom.Infrastructure { @@ -15,28 +16,28 @@ namespace Unity.BossRoom.Infrastructure /// public class NetworkObjectPool : NetworkBehaviour { - private static NetworkObjectPool _instance; + static NetworkObjectPool s_Instance; - public static NetworkObjectPool Singleton { get { return _instance; } } + public static NetworkObjectPool Singleton { get { return s_Instance; } } [SerializeField] List PooledPrefabsList; - HashSet prefabs = new HashSet(); + HashSet m_Prefabs = new HashSet(); - Dictionary> pooledObjects = new Dictionary>(); + Dictionary> m_PooledObjects = new Dictionary>(); - private bool m_HasInitialized = false; + bool m_HasInitialized = false; public void Awake() { - if (_instance != null && _instance != this) + if (s_Instance != null && s_Instance != this) { - Destroy(this.gameObject); + Destroy(gameObject); } else { - _instance = this; + s_Instance = this; } } @@ -91,7 +92,7 @@ public void ReturnNetworkObject(NetworkObject networkObject, GameObject prefab) { var go = networkObject.gameObject; go.SetActive(false); - pooledObjects[prefab].Enqueue(networkObject); + m_PooledObjects[prefab].Release(networkObject); } /// @@ -104,7 +105,7 @@ public void AddPrefab(GameObject prefab, int prewarmCount = 0) var networkObject = prefab.GetComponent(); Assert.IsNotNull(networkObject, $"{nameof(prefab)} must have {nameof(networkObject)} component."); - Assert.IsFalse(prefabs.Contains(prefab), $"Prefab {prefab.name} is already registered in the pool."); + Assert.IsFalse(m_Prefabs.Contains(prefab), $"Prefab {prefab.name} is already registered in the pool."); RegisterPrefabInternal(prefab, prewarmCount); } @@ -114,22 +115,17 @@ public void AddPrefab(GameObject prefab, int prewarmCount = 0) /// private void RegisterPrefabInternal(GameObject prefab, int prewarmCount) { - prefabs.Add(prefab); + m_Prefabs.Add(prefab); - var prefabQueue = new Queue(); - pooledObjects[prefab] = prefabQueue; - for (int i = 0; i < prewarmCount; i++) - { - var go = CreateInstance(prefab); - ReturnNetworkObject(go.GetComponent(), prefab); - } + var prefabPool = new ObjectPool((() => CreateInstance(prefab).GetComponent()), defaultCapacity: prewarmCount); + m_PooledObjects[prefab] = prefabPool; // Register Netcode Spawn handlers NetworkManager.Singleton.PrefabHandler.AddHandler(prefab, new PooledPrefabInstanceHandler(prefab, this)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private GameObject CreateInstance(GameObject prefab) + GameObject CreateInstance(GameObject prefab) { return Instantiate(prefab); } @@ -141,19 +137,9 @@ private GameObject CreateInstance(GameObject prefab) /// /// /// - private NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quaternion rotation) + NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quaternion rotation) { - var queue = pooledObjects[prefab]; - - NetworkObject networkObject; - if (queue.Count > 0) - { - networkObject = queue.Dequeue(); - } - else - { - networkObject = CreateInstance(prefab).GetComponent(); - } + var networkObject = m_PooledObjects[prefab].Get(); // Here we must reverse the logic in ReturnNetworkObject. var go = networkObject.gameObject; @@ -183,12 +169,12 @@ public void InitializePool() /// public void ClearPool() { - foreach (var prefab in prefabs) + foreach (var prefab in m_Prefabs) { // Unregister Netcode Spawn handlers NetworkManager.Singleton.PrefabHandler.RemoveHandler(prefab); } - pooledObjects.Clear(); + m_PooledObjects.Clear(); } } From 69094c11a15d645c8ab3cea67d02e72b0133422b Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 14:22:16 -0400 Subject: [PATCH 02/12] cleaning up and updating comments --- .../Scripts/Infrastructure/NetworkObjectPool.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 84339deba..a844a2e80 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -9,16 +9,15 @@ namespace Unity.BossRoom.Infrastructure { /// - /// Object Pool for networked objects, used for controlling how objects are spawned by Netcode. Netcode by default will allocate new memory when spawning new - /// objects. With this Networked Pool, we're using custom spawning to reuse objects. + /// Object Pool for networked objects, used for controlling how objects are spawned by Netcode. Netcode by default + /// will allocate new memory when spawning new objects. With this Networked Pool, we're using the ObjectPool to + /// reuse objects. /// Boss Room uses this for projectiles. In theory it should use this for imps too, but we wanted to show vanilla spawning vs pooled spawning. - /// Hooks to NetworkManager's prefab handler to intercept object spawning and do custom actions + /// Hooks to NetworkManager's prefab handler to intercept object spawning and do custom actions. /// public class NetworkObjectPool : NetworkBehaviour { - static NetworkObjectPool s_Instance; - - public static NetworkObjectPool Singleton { get { return s_Instance; } } + public static NetworkObjectPool Singleton { get; private set; } [SerializeField] List PooledPrefabsList; @@ -31,13 +30,13 @@ public class NetworkObjectPool : NetworkBehaviour public void Awake() { - if (s_Instance != null && s_Instance != this) + if (Singleton != null && Singleton != this) { Destroy(gameObject); } else { - s_Instance = this; + Singleton = this; } } @@ -113,7 +112,7 @@ public void AddPrefab(GameObject prefab, int prewarmCount = 0) /// /// Builds up the cache for a prefab. /// - private void RegisterPrefabInternal(GameObject prefab, int prewarmCount) + void RegisterPrefabInternal(GameObject prefab, int prewarmCount) { m_Prefabs.Add(prefab); From 232d33d9f6a23a15fa84c37cd060f2928b01c50d Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 14:51:44 -0400 Subject: [PATCH 03/12] reimplementing the prewarming of the pool --- .../Infrastructure/NetworkObjectPool.cs | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index a844a2e80..dffa39f86 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -89,8 +89,6 @@ public NetworkObject GetNetworkObject(GameObject prefab, Vector3 position, Quate /// public void ReturnNetworkObject(NetworkObject networkObject, GameObject prefab) { - var go = networkObject.gameObject; - go.SetActive(false); m_PooledObjects[prefab].Release(networkObject); } @@ -116,7 +114,30 @@ void RegisterPrefabInternal(GameObject prefab, int prewarmCount) { m_Prefabs.Add(prefab); - var prefabPool = new ObjectPool((() => CreateInstance(prefab).GetComponent()), defaultCapacity: prewarmCount); + NetworkObject CreateFunc() + { + return CreateInstance(prefab).GetComponent(); + } + + void ActionOnGet(NetworkObject networkObject) + { + var go = networkObject.gameObject; + go.SetActive(true); + } + + void ActionOnRelease(NetworkObject networkObject) + { + var go = networkObject.gameObject; + go.SetActive(false); + } + + var prefabPool = new ObjectPool((CreateFunc), ActionOnGet, ActionOnRelease, defaultCapacity: prewarmCount); + + // Populate the pool + for (var i = 0; i < prewarmCount; i++) + { + ReturnNetworkObject(CreateFunc(), prefab); + } m_PooledObjects[prefab] = prefabPool; // Register Netcode Spawn handlers @@ -140,12 +161,8 @@ NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quat { var networkObject = m_PooledObjects[prefab].Get(); - // Here we must reverse the logic in ReturnNetworkObject. - var go = networkObject.gameObject; - go.SetActive(true); - - go.transform.position = position; - go.transform.rotation = rotation; + networkObject.transform.position = position; + networkObject.transform.rotation = rotation; return networkObject; } @@ -174,6 +191,8 @@ public void ClearPool() NetworkManager.Singleton.PrefabHandler.RemoveHandler(prefab); } m_PooledObjects.Clear(); + m_Prefabs.Clear(); + m_HasInitialized = false; } } From f90b64c9fb6e6552f824de9ef3f6f65eb41d2227 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 14:58:24 -0400 Subject: [PATCH 04/12] removing unnecessary external public apis --- Assets/Scripts/Infrastructure/NetworkObjectPool.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index dffa39f86..6774fe21e 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -26,8 +26,6 @@ public class NetworkObjectPool : NetworkBehaviour Dictionary> m_PooledObjects = new Dictionary>(); - bool m_HasInitialized = false; - public void Awake() { if (Singleton != null && Singleton != this) @@ -170,20 +168,18 @@ NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quat /// /// Registers all objects in to the cache. /// - public void InitializePool() + void InitializePool() { - if (m_HasInitialized) return; foreach (var configObject in PooledPrefabsList) { RegisterPrefabInternal(configObject.Prefab, configObject.PrewarmCount); } - m_HasInitialized = true; } /// /// Unregisters all objects in from the cache. /// - public void ClearPool() + void ClearPool() { foreach (var prefab in m_Prefabs) { @@ -192,7 +188,6 @@ public void ClearPool() } m_PooledObjects.Clear(); m_Prefabs.Clear(); - m_HasInitialized = false; } } From fb4c2e11aa34122812652cb39fe44441c987bf39 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 15:24:53 -0400 Subject: [PATCH 05/12] fixing bugs --- .../Scripts/Infrastructure/NetworkObjectPool.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 6774fe21e..711860e75 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -129,14 +129,24 @@ void ActionOnRelease(NetworkObject networkObject) go.SetActive(false); } - var prefabPool = new ObjectPool((CreateFunc), ActionOnGet, ActionOnRelease, defaultCapacity: prewarmCount); + void ActionOnDestroy(NetworkObject networkObject) + { + Destroy(networkObject.gameObject); + } + + m_PooledObjects[prefab] = new ObjectPool((CreateFunc), ActionOnGet, ActionOnRelease, ActionOnDestroy, defaultCapacity: prewarmCount); // Populate the pool + var prewarmNetworkObjects = new List(); for (var i = 0; i < prewarmCount; i++) { - ReturnNetworkObject(CreateFunc(), prefab); + prewarmNetworkObjects.Add(m_PooledObjects[prefab].Get()); + } + + foreach (var networkObject in prewarmNetworkObjects) + { + m_PooledObjects[prefab].Release(networkObject); } - m_PooledObjects[prefab] = prefabPool; // Register Netcode Spawn handlers NetworkManager.Singleton.PrefabHandler.AddHandler(prefab, new PooledPrefabInstanceHandler(prefab, this)); @@ -185,6 +195,7 @@ void ClearPool() { // Unregister Netcode Spawn handlers NetworkManager.Singleton.PrefabHandler.RemoveHandler(prefab); + m_PooledObjects[prefab].Clear(); } m_PooledObjects.Clear(); m_Prefabs.Clear(); From cbf0deaa653cb7826f861a4590de464cd7f37c71 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 15:35:01 -0400 Subject: [PATCH 06/12] Adding changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bc32d931..b21e50af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Changed * Replaced our polling for lobby updates with a subscription to the new Websocket based LobbyEvents (#805). This saves up a significant amount of bandwidth usage to and from the service, since updates are infrequent in this game. Now clients and hosts only use up bandwidth on the Lobby service when it is needed. With polling, we used to send a GET request per client once every 2s. The responses were between ~550 bytes and 900 bytes, so if we suppose an average of 725 bytes and 100 000 concurrent users (CCU), this amounted to around 725B * 30 calls per minute * 100 000 CCU = 2.175 GB per minute. Scaling this to a month would get us 93.96 TB per month. In our case, since the only changes to the lobbies happen when a user connects or disconnects, most of that data was not necessary and can be saved to reduce bandwidth usage. Since the cost of using the Lobby service depends on bandwidth usage, this would also save money on an actual game. +* Replaced our custom pool implementation using queues with ObjectPool (#824) * ### Cleanup * Clarified a TODO comment inside ClientCharacter, detailing how anticipation should only be executed on owning client players (#786) From 2ea5f2aca763b8389f68bafdfb5a8fa174dd50db Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 16:13:15 -0400 Subject: [PATCH 07/12] cleanup --- Assets/Scripts/Infrastructure/NetworkObjectPool.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 711860e75..204d80978 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -110,8 +110,6 @@ public void AddPrefab(GameObject prefab, int prewarmCount = 0) /// void RegisterPrefabInternal(GameObject prefab, int prewarmCount) { - m_Prefabs.Add(prefab); - NetworkObject CreateFunc() { return CreateInstance(prefab).GetComponent(); @@ -134,6 +132,9 @@ void ActionOnDestroy(NetworkObject networkObject) Destroy(networkObject.gameObject); } + m_Prefabs.Add(prefab); + + // Create the pool m_PooledObjects[prefab] = new ObjectPool((CreateFunc), ActionOnGet, ActionOnRelease, ActionOnDestroy, defaultCapacity: prewarmCount); // Populate the pool @@ -142,7 +143,6 @@ void ActionOnDestroy(NetworkObject networkObject) { prewarmNetworkObjects.Add(m_PooledObjects[prefab].Get()); } - foreach (var networkObject in prewarmNetworkObjects) { m_PooledObjects[prefab].Release(networkObject); From b3e9afc2e02cbc1bd534293ae8a83e2b84bd4198 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Thu, 20 Apr 2023 16:42:16 -0400 Subject: [PATCH 08/12] reordering changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eb21c38c..a71278746 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,8 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Changed * Replaced our polling for lobby updates with a subscription to the new Websocket based LobbyEvents (#805). This saves up a significant amount of bandwidth usage to and from the service, since updates are infrequent in this game. Now clients and hosts only use up bandwidth on the Lobby service when it is needed. With polling, we used to send a GET request per client once every 2s. The responses were between ~550 bytes and 900 bytes, so if we suppose an average of 725 bytes and 100 000 concurrent users (CCU), this amounted to around 725B * 30 calls per minute * 100 000 CCU = 2.175 GB per minute. Scaling this to a month would get us 93.96 TB per month. In our case, since the only changes to the lobbies happen when a user connects or disconnects, most of that data was not necessary and can be saved to reduce bandwidth usage. Since the cost of using the Lobby service depends on bandwidth usage, this would also save money on an actual game. -* Replaced our custom pool implementation using queues with ObjectPool (#824) * Simplified reconnection flow by offloading responsibility to ConnectionMethod (#804). Now the ClientReconnectingState uses the ConnectionMethod it is configured with to handle setting up reconnection (i.e. reconnecting to the Lobby before trying to reconnect to the Relay server if it is using Relay and Lobby). It can now also fail early and stop retrying if the lobby doesn't exist anymore. +* Replaced our custom pool implementation using queues with ObjectPool (#824) ### Cleanup * Clarified a TODO comment inside ClientCharacter, detailing how anticipation should only be executed on owning client players (#786) From 5b2c4021a214517348a271de684aca17c706aa84 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Fri, 21 Apr 2023 11:54:13 -0400 Subject: [PATCH 09/12] cleaning up unused methods --- .../Infrastructure/NetworkObjectPool.cs | 33 +------------------ 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 204d80978..515a67305 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -60,16 +60,6 @@ public void OnValidate() } } - /// - /// Gets an instance of the given prefab from the pool. The prefab must be registered to the pool. - /// - /// - /// - public NetworkObject GetNetworkObject(GameObject prefab) - { - return GetNetworkObjectInternal(prefab, Vector3.zero, Quaternion.identity); - } - /// /// Gets an instance of the given prefab from the pool. The prefab must be registered to the pool. /// @@ -90,21 +80,6 @@ public void ReturnNetworkObject(NetworkObject networkObject, GameObject prefab) m_PooledObjects[prefab].Release(networkObject); } - /// - /// Adds a prefab to the list of spawnable prefabs. - /// - /// The prefab to add. - /// - public void AddPrefab(GameObject prefab, int prewarmCount = 0) - { - var networkObject = prefab.GetComponent(); - - Assert.IsNotNull(networkObject, $"{nameof(prefab)} must have {nameof(networkObject)} component."); - Assert.IsFalse(m_Prefabs.Contains(prefab), $"Prefab {prefab.name} is already registered in the pool."); - - RegisterPrefabInternal(prefab, prewarmCount); - } - /// /// Builds up the cache for a prefab. /// @@ -112,7 +87,7 @@ void RegisterPrefabInternal(GameObject prefab, int prewarmCount) { NetworkObject CreateFunc() { - return CreateInstance(prefab).GetComponent(); + return Instantiate(prefab).GetComponent(); } void ActionOnGet(NetworkObject networkObject) @@ -152,12 +127,6 @@ void ActionOnDestroy(NetworkObject networkObject) NetworkManager.Singleton.PrefabHandler.AddHandler(prefab, new PooledPrefabInstanceHandler(prefab, this)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - GameObject CreateInstance(GameObject prefab) - { - return Instantiate(prefab); - } - /// /// This matches the signature of /// From e895a6fa61b487fb93bb4538d11a4c9fd4f25f81 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Fri, 21 Apr 2023 12:30:42 -0400 Subject: [PATCH 10/12] more cleanup --- .../Infrastructure/NetworkObjectPool.cs | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 515a67305..db00aae04 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using Unity.Netcode; using UnityEngine; using UnityEngine.Assertions; @@ -40,12 +39,24 @@ public void Awake() public override void OnNetworkSpawn() { - InitializePool(); + // Registers all objects in PooledPrefabsList to the cache. + foreach (var configObject in PooledPrefabsList) + { + RegisterPrefabInternal(configObject.Prefab, configObject.PrewarmCount); + } } public override void OnNetworkDespawn() { - ClearPool(); + // Unregisters all objects in PooledPrefabsList from the cache. + foreach (var prefab in m_Prefabs) + { + // Unregister Netcode Spawn handlers + NetworkManager.Singleton.PrefabHandler.RemoveHandler(prefab); + m_PooledObjects[prefab].Clear(); + } + m_PooledObjects.Clear(); + m_Prefabs.Clear(); } public void OnValidate() @@ -110,7 +121,7 @@ void ActionOnDestroy(NetworkObject networkObject) m_Prefabs.Add(prefab); // Create the pool - m_PooledObjects[prefab] = new ObjectPool((CreateFunc), ActionOnGet, ActionOnRelease, ActionOnDestroy, defaultCapacity: prewarmCount); + m_PooledObjects[prefab] = new ObjectPool(CreateFunc, ActionOnGet, ActionOnRelease, ActionOnDestroy, defaultCapacity: prewarmCount); // Populate the pool var prewarmNetworkObjects = new List(); @@ -143,32 +154,6 @@ NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quat return networkObject; } - - /// - /// Registers all objects in to the cache. - /// - void InitializePool() - { - foreach (var configObject in PooledPrefabsList) - { - RegisterPrefabInternal(configObject.Prefab, configObject.PrewarmCount); - } - } - - /// - /// Unregisters all objects in from the cache. - /// - void ClearPool() - { - foreach (var prefab in m_Prefabs) - { - // Unregister Netcode Spawn handlers - NetworkManager.Singleton.PrefabHandler.RemoveHandler(prefab); - m_PooledObjects[prefab].Clear(); - } - m_PooledObjects.Clear(); - m_Prefabs.Clear(); - } } [Serializable] From db1b21d526e51b14e835b1ed36b1999a09c9ce24 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Fri, 21 Apr 2023 12:34:13 -0400 Subject: [PATCH 11/12] more cleanup again --- Assets/Scripts/Infrastructure/NetworkObjectPool.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index db00aae04..744af2804 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -103,14 +103,12 @@ NetworkObject CreateFunc() void ActionOnGet(NetworkObject networkObject) { - var go = networkObject.gameObject; - go.SetActive(true); + networkObject.gameObject.SetActive(true); } void ActionOnRelease(NetworkObject networkObject) { - var go = networkObject.gameObject; - go.SetActive(false); + networkObject.gameObject.SetActive(false); } void ActionOnDestroy(NetworkObject networkObject) @@ -149,8 +147,9 @@ NetworkObject GetNetworkObjectInternal(GameObject prefab, Vector3 position, Quat { var networkObject = m_PooledObjects[prefab].Get(); - networkObject.transform.position = position; - networkObject.transform.rotation = rotation; + var noTransform = networkObject.transform; + noTransform.position = position; + noTransform.rotation = rotation; return networkObject; } @@ -176,8 +175,7 @@ public PooledPrefabInstanceHandler(GameObject prefab, NetworkObjectPool pool) NetworkObject INetworkPrefabInstanceHandler.Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) { - var netObject = m_Pool.GetNetworkObject(m_Prefab, position, rotation); - return netObject; + return m_Pool.GetNetworkObject(m_Prefab, position, rotation); } void INetworkPrefabInstanceHandler.Destroy(NetworkObject networkObject) From 93cf1f4a3028a2d056c8469aabd068b00ddab34d Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Fri, 21 Apr 2023 12:49:07 -0400 Subject: [PATCH 12/12] Adding comments --- Assets/Scripts/Infrastructure/NetworkObjectPool.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs index 744af2804..ea3dcf59a 100644 --- a/Assets/Scripts/Infrastructure/NetworkObjectPool.cs +++ b/Assets/Scripts/Infrastructure/NetworkObjectPool.cs @@ -74,6 +74,12 @@ public void OnValidate() /// /// Gets an instance of the given prefab from the pool. The prefab must be registered to the pool. /// + /// + /// To spawn a NetworkObject from one of the pools, this must be called on the server, then the instance + /// returned from it must be spawned on the server. This method will then also be called on the client by the + /// PooledPrefabInstanceHandler when the client receives a spawn message for a prefab that has been registered + /// here. + /// /// /// The position to spawn the object at. /// The rotation to spawn the object with.