Skip to content

Commit fb1555f

Browse files
feat!: Network Time (RFC #14) (#845)
* refactor: simplify tick system, introduce server based time * feat!: remove receive tick * feat!: remove max received events per tick rate * feat!: Update components to use new time system. Remove time sync message. * test: NetworkTime operators and creation tests * test: Upgrade manual network variable test to new time system * docs: network config tooltip for tickrate * feat!: completely remove old network time * feat: decouple network time system * feat: NetworkTime use double everywhere * docs: Improve tick rate tool tip. * change tickrate to 30 Co-authored-by: Matt Walsh <69258106+mattwalsh-unity@users.noreply.github.com>
1 parent 9ae52de commit fb1555f

File tree

53 files changed

+1487
-445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1487
-445
lines changed

com.unity.multiplayer.mlapi/Editor/NetworkManagerEditor.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ public class NetworkManagerEditor : UnityEditor.Editor
2525
private SerializedProperty m_ProtocolVersionProperty;
2626
private SerializedProperty m_AllowRuntimeSceneChangesProperty;
2727
private SerializedProperty m_NetworkTransportProperty;
28-
private SerializedProperty m_ReceiveTickrateProperty;
29-
private SerializedProperty m_NetworkTickIntervalSecProperty;
30-
private SerializedProperty m_MaxReceiveEventsPerTickRateProperty;
31-
private SerializedProperty m_EventTickrateProperty;
28+
private SerializedProperty m_TickRateProperty;
3229
private SerializedProperty m_MaxObjectUpdatesPerTickProperty;
3330
private SerializedProperty m_ClientConnectionBufferTimeoutProperty;
3431
private SerializedProperty m_ConnectionApprovalProperty;
35-
private SerializedProperty m_EnableTimeResyncProperty;
36-
private SerializedProperty m_TimeResyncIntervalProperty;
3732
private SerializedProperty m_EnableNetworkVariableProperty;
3833
private SerializedProperty m_EnsureNetworkVariableLengthSafetyProperty;
3934
private SerializedProperty m_ForceSamePrefabsProperty;
@@ -103,14 +98,9 @@ private void Initialize()
10398
m_ProtocolVersionProperty = m_NetworkConfigProperty.FindPropertyRelative("ProtocolVersion");
10499
m_AllowRuntimeSceneChangesProperty = m_NetworkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
105100
m_NetworkTransportProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTransport");
106-
m_ReceiveTickrateProperty = m_NetworkConfigProperty.FindPropertyRelative("ReceiveTickrate");
107-
m_NetworkTickIntervalSecProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTickIntervalSec");
108-
m_MaxReceiveEventsPerTickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
109-
m_EventTickrateProperty = m_NetworkConfigProperty.FindPropertyRelative("EventTickrate");
101+
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
110102
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
111103
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
112-
m_EnableTimeResyncProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableTimeResync");
113-
m_TimeResyncIntervalProperty = m_NetworkConfigProperty.FindPropertyRelative("TimeResyncInterval");
114104
m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable");
115105
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
116106
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
@@ -139,14 +129,9 @@ private void CheckNullProperties()
139129
m_ProtocolVersionProperty = m_NetworkConfigProperty.FindPropertyRelative("ProtocolVersion");
140130
m_AllowRuntimeSceneChangesProperty = m_NetworkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
141131
m_NetworkTransportProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTransport");
142-
m_ReceiveTickrateProperty = m_NetworkConfigProperty.FindPropertyRelative("ReceiveTickrate");
143-
m_NetworkTickIntervalSecProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTickIntervalSec");
144-
m_MaxReceiveEventsPerTickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
145-
m_EventTickrateProperty = m_NetworkConfigProperty.FindPropertyRelative("EventTickrate");
132+
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
146133
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
147134
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
148-
m_EnableTimeResyncProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableTimeResync");
149-
m_TimeResyncIntervalProperty = m_NetworkConfigProperty.FindPropertyRelative("TimeResyncInterval");
150135
m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable");
151136
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
152137
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
@@ -304,27 +289,13 @@ public override void OnInspectorGUI()
304289
}
305290
}
306291

307-
EditorGUILayout.PropertyField(m_EnableTimeResyncProperty);
308-
309-
using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.EnableTimeResync))
310-
{
311-
EditorGUILayout.PropertyField(m_TimeResyncIntervalProperty);
312-
}
292+
EditorGUILayout.PropertyField(m_TickRateProperty);
313293

314294
EditorGUILayout.LabelField("Performance", EditorStyles.boldLabel);
315-
EditorGUILayout.PropertyField(m_ReceiveTickrateProperty);
316-
EditorGUILayout.PropertyField(m_NetworkTickIntervalSecProperty);
317-
EditorGUILayout.PropertyField(m_MaxReceiveEventsPerTickRateProperty);
318-
EditorGUILayout.PropertyField(m_EventTickrateProperty);
319295
EditorGUILayout.PropertyField(m_EnableNetworkVariableProperty);
320296

321297
using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.EnableNetworkVariable))
322298
{
323-
if (m_MaxObjectUpdatesPerTickProperty != null)
324-
{
325-
EditorGUILayout.PropertyField(m_MaxObjectUpdatesPerTickProperty);
326-
}
327-
328299
EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty);
329300
}
330301

com.unity.multiplayer.mlapi/Prototyping/NetworkAnimator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public enum Authority
247247

248248
[SerializeField]
249249
private float m_SendRate = 0.1f;
250-
private float m_NextSendTime = 0.0f;
250+
private double m_NextSendTime = 0.0f;
251251
private bool m_ServerRequestsAnimationResync = false;
252252
[SerializeField]
253253
private Animator m_Animator;
@@ -270,7 +270,7 @@ public override void OnNetworkSpawn()
270270

271271
if (m_Animator.IsParameterControlledByCurve(parameter.nameHash))
272272
{
273-
//we are ignoring parameters that are controlled by animation curves - syncing the layer states indirectly syncs the values that are driven by the animation curves
273+
//we are ignoring parameters that are controlled by animation curves - syncing the layer states indirectly syncs the values that are driven by the animation curves
274274
continue;
275275
}
276276

@@ -373,7 +373,7 @@ private void FixedUpdate()
373373

374374
private bool CheckSendRate()
375375
{
376-
var networkTime = NetworkManager.NetworkTime;
376+
var networkTime = NetworkManager.LocalTime.FixedTime;
377377
if (m_SendRate != 0 && m_NextSendTime < networkTime)
378378
{
379379
m_NextSendTime = networkTime + m_SendRate;

com.unity.multiplayer.mlapi/Prototyping/NetworkNavMeshAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private void Awake()
4848
}
4949

5050
private Vector3 m_LastDestination = Vector3.zero;
51-
private float m_LastCorrectionTime = 0f;
51+
private double m_LastCorrectionTime = 0d;
5252

5353
private void Update()
5454
{
@@ -79,7 +79,7 @@ private void Update()
7979
}
8080
}
8181

82-
if (NetworkManager.NetworkTime - m_LastCorrectionTime >= CorrectionDelay)
82+
if (NetworkManager.LocalTime.Time - m_LastCorrectionTime >= CorrectionDelay) // TODO this is not aliased correctly, is this an issue?
8383
{
8484
if (!EnableProximity)
8585
{
@@ -99,7 +99,7 @@ private void Update()
9999
OnNavMeshCorrectionUpdateClientRpc(m_Agent.velocity, transform.position, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = proximityClients.ToArray() } });
100100
}
101101

102-
m_LastCorrectionTime = NetworkManager.NetworkTime;
102+
m_LastCorrectionTime = NetworkManager.LocalTime.Time;
103103
}
104104
}
105105

com.unity.multiplayer.mlapi/Runtime/Configuration/NetworkConfig.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,10 @@ public class NetworkConfig
6161
internal Dictionary<uint, NetworkPrefab> NetworkPrefabOverrideLinks = new Dictionary<uint, NetworkPrefab>();
6262

6363
/// <summary>
64-
/// Amount of times per second the receive queue is emptied and all messages inside are processed.
64+
/// The tickrate of network ticks. This value controls how often MLAPI runs user code and sends out data.
6565
/// </summary>
66-
[Tooltip("The amount of times per second the receive queue is emptied from pending incoming messages")]
67-
public int ReceiveTickrate = 64;
68-
69-
/// <summary>
70-
/// Duration in seconds between network ticks.
71-
/// </summary>
72-
[Tooltip("Duration in seconds between network ticks")]
73-
public float NetworkTickIntervalSec = 0.050f;
74-
75-
/// <summary>
76-
/// The max amount of messages to process per ReceiveTickrate. This is to prevent flooding.
77-
/// </summary>
78-
[Tooltip("The maximum amount of Receive events to poll per Receive tick. This is to prevent flooding and freezing on the server")]
79-
public int MaxReceiveEventsPerTickRate = 500;
80-
81-
/// <summary>
82-
/// The amount of times per second internal frame events will occur, e.g. send checking.
83-
/// </summary>
84-
[Tooltip("The amount of times per second the internal event loop will run. This includes for example NetworkVariable checking.")]
85-
public int EventTickrate = 64;
66+
[Tooltip("The tickrate. This value controls how often MLAPI runs user code and sends out data. The value is in 'ticks per seconds' which means a value of 50 will result in 50 ticks being executed per second or a fixed delta time of 0.02.")]
67+
public int TickRate = 30;
8668

8769
/// <summary>
8870
/// The amount of seconds to wait for handshake to complete before timing out a client
@@ -206,9 +188,7 @@ public string ToBase64()
206188
writer.WriteString(config.RegisteredScenes[i]);
207189
}
208190

209-
writer.WriteInt32Packed(config.ReceiveTickrate);
210-
writer.WriteInt32Packed(config.MaxReceiveEventsPerTickRate);
211-
writer.WriteInt32Packed(config.EventTickrate);
191+
writer.WriteInt32Packed(config.TickRate);
212192
writer.WriteInt32Packed(config.ClientConnectionBufferTimeout);
213193
writer.WriteBool(config.ConnectionApproval);
214194
writer.WriteInt32Packed(config.LoadSceneTimeOut);
@@ -249,9 +229,7 @@ public void FromBase64(string base64)
249229
config.RegisteredScenes.Add(reader.ReadString().ToString());
250230
}
251231

252-
config.ReceiveTickrate = reader.ReadInt32Packed();
253-
config.MaxReceiveEventsPerTickRate = reader.ReadInt32Packed();
254-
config.EventTickrate = reader.ReadInt32Packed();
232+
config.TickRate = reader.ReadInt32Packed();
255233
config.ClientConnectionBufferTimeout = reader.ReadInt32Packed();
256234
config.ConnectionApproval = reader.ReadBool();
257235
config.LoadSceneTimeOut = reader.ReadInt32Packed();

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using MLAPI.Configuration;
88
using MLAPI.Logging;
99
using MLAPI.Messaging;
10+
using MLAPI.Timing;
1011
using MLAPI.NetworkVariable;
1112
using MLAPI.Profiling;
1213
using MLAPI.Reflection;
@@ -296,7 +297,6 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId)
296297
/// </summary>
297298
public ulong OwnerClientId => NetworkObject.OwnerClientId;
298299

299-
300300
/// <summary>
301301
/// Gets called when message handlers are ready to be registered and the network is setup
302302
/// </summary>

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviourUpdater.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,12 @@ public class NetworkBehaviourUpdater
77
{
88
private HashSet<NetworkObject> m_Touched = new HashSet<NetworkObject>();
99

10-
/// <summary>
11-
/// Stores the network tick at the NetworkBehaviourUpdate time
12-
/// This allows sending NetworkVariables not more often than once per network tick, regardless of the update rate
13-
/// </summary>
14-
public ushort CurrentTick { get; set; }
15-
1610
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1711
private ProfilerMarker m_NetworkBehaviourUpdate = new ProfilerMarker($"{nameof(NetworkBehaviour)}.{nameof(NetworkBehaviourUpdate)}");
1812
#endif
1913

2014
internal void NetworkBehaviourUpdate(NetworkManager networkManager)
2115
{
22-
// Do not execute NetworkBehaviourUpdate more than once per network tick
23-
ushort tick = networkManager.NetworkTickSystem.GetTick();
24-
if (tick == CurrentTick)
25-
{
26-
return;
27-
}
28-
29-
CurrentTick = tick;
30-
3116
#if DEVELOPMENT_BUILD || UNITY_EDITOR
3217
m_NetworkBehaviourUpdate.Begin();
3318
#endif

0 commit comments

Comments
 (0)