Skip to content

Commit 805e584

Browse files
committed
fix(networked-var): Fixes networked var sync when object is not spawned
This also makes object spawning faster Fixes: #210
1 parent 20dc094 commit 805e584

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

MLAPI-Editor/NetworkingManagerEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class NetworkingManagerEditor : Editor
2727
private SerializedProperty maxReceiveEventsPerTickRateProperty;
2828
private SerializedProperty sendTickrateProperty;
2929
private SerializedProperty eventTickrateProperty;
30-
private SerializedProperty maxBehaviourUpdatesPerTickProperty;
30+
private SerializedProperty maxObjectUpdatesPerTickProperty;
3131
private SerializedProperty clientConnectionBufferTimeoutProperty;
3232
private SerializedProperty connectionApprovalProperty;
3333
private SerializedProperty secondsHistoryProperty;
@@ -100,7 +100,7 @@ private void Init()
100100
maxReceiveEventsPerTickRateProperty = networkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
101101
sendTickrateProperty = networkConfigProperty.FindPropertyRelative("SendTickrate");
102102
eventTickrateProperty = networkConfigProperty.FindPropertyRelative("EventTickrate");
103-
maxBehaviourUpdatesPerTickProperty = networkConfigProperty.FindPropertyRelative("MaxBehaviourUpdatesPerTick");
103+
maxObjectUpdatesPerTickProperty = networkConfigProperty.FindPropertyRelative("MaxObjectUpdatesPerTick");
104104
clientConnectionBufferTimeoutProperty = networkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
105105
connectionApprovalProperty = networkConfigProperty.FindPropertyRelative("ConnectionApproval");
106106
secondsHistoryProperty = networkConfigProperty.FindPropertyRelative("SecondsHistory");
@@ -133,7 +133,7 @@ private void CheckNullProperties()
133133
maxReceiveEventsPerTickRateProperty = networkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
134134
sendTickrateProperty = networkConfigProperty.FindPropertyRelative("SendTickrate");
135135
eventTickrateProperty = networkConfigProperty.FindPropertyRelative("EventTickrate");
136-
maxBehaviourUpdatesPerTickProperty = networkConfigProperty.FindPropertyRelative("MaxBehaviourUpdatesPerTick");
136+
maxObjectUpdatesPerTickProperty = networkConfigProperty.FindPropertyRelative("MaxObjectUpdatesPerTick");
137137
clientConnectionBufferTimeoutProperty = networkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
138138
connectionApprovalProperty = networkConfigProperty.FindPropertyRelative("ConnectionApproval");
139139
secondsHistoryProperty = networkConfigProperty.FindPropertyRelative("SecondsHistory");
@@ -278,7 +278,7 @@ public override void OnInspectorGUI()
278278

279279
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableNetworkedVar))
280280
{
281-
EditorGUILayout.PropertyField(maxBehaviourUpdatesPerTickProperty);
281+
EditorGUILayout.PropertyField(maxObjectUpdatesPerTickProperty);
282282
}
283283

284284
EditorGUILayout.LabelField("Connection", EditorStyles.boldLabel);

MLAPI/Data/NetworkConfig.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using MLAPI.Transports;
88
using BitStream = MLAPI.Serialization.BitStream;
99
using System.Security.Cryptography.X509Certificates;
10+
using UnityEngine.Serialization;
1011

1112
namespace MLAPI.Configuration
1213
{
@@ -62,12 +63,13 @@ public class NetworkConfig
6263
[Tooltip("The amount of times per second the internal event loop will run. This includes for example SyncedVar checking and LagCompensation tracking")]
6364
public int EventTickrate = 64;
6465
/// <summary>
65-
/// The maximum amount of NetworkedBehaviour's to process per tick.
66+
/// The maximum amount of NetworkedObject's to process per tick.
6667
/// This is useful to prevent the MLAPI from hanging a frame
6768
/// Set this to less than or equal to 0 for unlimited
6869
/// </summary>
69-
[Tooltip("The maximum amount of NetworkedBehaviour SyncedVars to process per Event tick. This is to prevent freezing")]
70-
public int MaxBehaviourUpdatesPerTick = -1;
70+
[FormerlySerializedAs("MaxBehaviourUpdatesPerTick")]
71+
[Tooltip("The maximum amount of NetworkedObject SyncedVars to process per Event tick. This is to prevent freezing")]
72+
public int MaxObjectUpdatesPerTick = -1;
7173
/// <summary>
7274
/// The amount of seconds to wait for handshake to complete before timing out a client
7375
/// </summary>

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ private void OnEnable()
118118
{
119119
if (_networkedObject == null)
120120
_networkedObject = GetComponentInParent<NetworkedObject>();
121-
122-
NetworkedObject.NetworkedBehaviours.Add(this);
121+
123122
OnEnabled();
124123
}
125124

@@ -130,7 +129,6 @@ private void OnDisable()
130129

131130
private void OnDestroy()
132131
{
133-
NetworkedObject.NetworkedBehaviours.Remove(this); // O(n)
134132
CachedClientRpcs.Remove(this);
135133
CachedServerRpcs.Remove(this);
136134
OnDestroyed();

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace MLAPI
1717
[AddComponentMenu("MLAPI/NetworkedObject", -99)]
1818
public sealed class NetworkedObject : MonoBehaviour
1919
{
20-
internal static readonly List<NetworkedBehaviour> NetworkedBehaviours = new List<NetworkedBehaviour>();
21-
2220
private void OnValidate()
2321
{
2422
if (string.IsNullOrEmpty(PrefabHashGenerator))
@@ -384,18 +382,21 @@ internal List<NetworkedBehaviour> childNetworkedBehaviours
384382
}
385383
}
386384

387-
private static int _lastProcessedBehaviour = 0;
385+
private static int _lastProcessedObject = 0;
388386
internal static void NetworkedBehaviourUpdate()
389387
{
390-
int amountToProcess = NetworkingManager.Singleton.NetworkConfig.MaxBehaviourUpdatesPerTick <= 0 ? NetworkedBehaviours.Count : Mathf.Max(NetworkingManager.Singleton.NetworkConfig.MaxBehaviourUpdatesPerTick, NetworkedBehaviours.Count);
388+
int amountToProcess = NetworkingManager.Singleton.NetworkConfig.MaxObjectUpdatesPerTick <= 0 ? SpawnManager.SpawnedObjectsList.Count : Mathf.Max(NetworkingManager.Singleton.NetworkConfig.MaxObjectUpdatesPerTick, SpawnManager.SpawnedObjectsList.Count);
391389

392390
for (int i = 0; i < amountToProcess; i++)
393391
{
394-
if (_lastProcessedBehaviour >= NetworkedBehaviours.Count)
395-
_lastProcessedBehaviour = 0;
396-
397-
NetworkedBehaviours[_lastProcessedBehaviour].NetworkedVarUpdate();
398-
_lastProcessedBehaviour++;
392+
if (_lastProcessedObject >= SpawnManager.SpawnedObjectsList.Count)
393+
_lastProcessedObject = 0;
394+
395+
// Sync all vars
396+
for (int j = 0; j < SpawnManager.SpawnedObjectsList[_lastProcessedObject].childNetworkedBehaviours.Count; j++)
397+
SpawnManager.SpawnedObjectsList[_lastProcessedObject].childNetworkedBehaviours[j].NetworkedVarUpdate();
398+
399+
_lastProcessedObject++;
399400
}
400401
}
401402

0 commit comments

Comments
 (0)