Skip to content

Commit a646273

Browse files
committed
feat: Allows disabling or delaying NetworkId recycle
Fixes: #214
1 parent ba5de3f commit a646273

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

MLAPI/Configuration/NetworkConfig.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ public class NetworkConfig
122122
"If false, Only non scene objects have to be prefabs. Scene objects will be matched using their PrefabInstanceId which can be precomputed globally for a scene at build time. Useful for single projects")]
123123
public bool UsePrefabSync = false;
124124
/// <summary>
125+
/// If true, NetworkIds will be reused after the NetworkIdRecycleDelay.
126+
/// </summary>
127+
[Tooltip("If true, NetworkIds will be reused after the NetworkIdRecycleDelay")]
128+
public bool RecycleNetworkIds = true;
129+
/// <summary>
130+
/// The amount of seconds a NetworkId has to be unused in order for it to be reused.
131+
/// </summary>
132+
[Tooltip("The amount of seconds a NetworkId has to unused in order for it to be reused")]
133+
public float NetworkIdRecycleDelay = 120f;
134+
/// <summary>
125135
/// Decides how many bytes to use for Rpc messaging. Leave this to 2 bytes unless you are facing hash collisions
126136
/// </summary>
127137
[Tooltip("The maximum amount of bytes to use for RPC messages. Leave this to 2 unless you are facing hash collisions")]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MLAPI.Spawning
2+
{
3+
internal struct ReleasedNetworkId
4+
{
5+
public ulong NetworkId;
6+
public float ReleaseTime;
7+
}
8+
}

MLAPI/Spawning/SpawnManager.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public static void RemoveCustomDestroyHandler(ulong prefabHash)
9696
customDestroyHandlers.Remove(prefabHash);
9797
}
9898

99-
internal static readonly Stack<ulong> releasedNetworkObjectIds = new Stack<ulong>();
99+
internal static readonly Queue<ReleasedNetworkId> releasedNetworkObjectIds = new Queue<ReleasedNetworkId>();
100100
private static ulong networkObjectIdCounter;
101101
internal static ulong GetNetworkObjectId()
102102
{
103-
if (releasedNetworkObjectIds.Count > 0)
103+
if (releasedNetworkObjectIds.Count > 0 && NetworkingManager.Singleton.NetworkConfig.RecycleNetworkIds && (Time.unscaledTime - releasedNetworkObjectIds.Peek().ReleaseTime) >= NetworkingManager.Singleton.NetworkConfig.NetworkIdRecycleDelay)
104104
{
105-
return releasedNetworkObjectIds.Pop();
105+
return releasedNetworkObjectIds.Dequeue().NetworkId;
106106
}
107107
else
108108
{
@@ -522,7 +522,14 @@ internal static void OnDestroyObject(ulong networkId, bool destroyGameObject)
522522

523523
if (NetworkingManager.Singleton != null && NetworkingManager.Singleton.IsServer)
524524
{
525-
releasedNetworkObjectIds.Push(networkId);
525+
if (NetworkingManager.Singleton.NetworkConfig.RecycleNetworkIds)
526+
{
527+
releasedNetworkObjectIds.Enqueue(new ReleasedNetworkId()
528+
{
529+
NetworkId = networkId,
530+
ReleaseTime = Time.unscaledTime
531+
});
532+
}
526533

527534
if (SpawnedObjects[networkId] != null)
528535
{

0 commit comments

Comments
 (0)