Skip to content

Commit 5a44e19

Browse files
committed
Added incremental behaviour update support
1 parent e6bec80 commit 5a44e19

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

MLAPI/Data/NetworkConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public class NetworkConfig
7070
/// </summary>
7171
public int EventTickrate = 64;
7272
/// <summary>
73+
/// The maximum amount of NetworkedBehaviour's to process per tick.
74+
/// This is useful to prevent the MLAPI from hanging a frame
75+
/// Set this to less than or equal to 0 for unlimited
76+
/// </summary>
77+
public int MaxBehaviourUpdatesPerTick = -1;
78+
/// <summary>
7379
/// The max amount of Clients that can connect.
7480
/// </summary>
7581
public int MaxConnections = 100;

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,18 @@ internal List<NetworkedBehaviour> childNetworkedBehaviours
336336
}
337337
}
338338

339-
internal static void NetworkedVarPrepareSend()
339+
private static int _lastProcessedBehaviour = 0;
340+
internal static void NetworkedBehaviourUpdate()
340341
{
341-
for (int i = 0; i < NetworkedBehaviours.Count; i++)
342+
int amountToProcess = NetworkingManager.Singleton.NetworkConfig.MaxBehaviourUpdatesPerTick <= 0 ? NetworkedBehaviours.Count : Mathf.Max(NetworkingManager.Singleton.NetworkConfig.MaxBehaviourUpdatesPerTick, NetworkedBehaviours.Count);
343+
344+
for (int i = 0; i < amountToProcess; i++)
342345
{
343-
NetworkedBehaviours[i].NetworkedVarUpdate();
346+
if (_lastProcessedBehaviour >= NetworkedBehaviours.Count)
347+
_lastProcessedBehaviour = 0;
348+
349+
NetworkedBehaviours[_lastProcessedBehaviour].NetworkedVarUpdate();
350+
_lastProcessedBehaviour++;
344351
}
345352
}
346353

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ private void Update()
600600
{
601601
if((NetworkTime - lastSendTickTime >= (1f / NetworkConfig.SendTickrate)) || NetworkConfig.SendTickrate <= 0)
602602
{
603-
NetworkedObject.NetworkedVarPrepareSend();
603+
NetworkedObject.NetworkedBehaviourUpdate();
604604
foreach (KeyValuePair<uint, NetworkedClient> pair in ConnectedClients)
605605
{
606606
byte error;

0 commit comments

Comments
 (0)