Skip to content

Commit a983537

Browse files
committed
feat: Network Tick System PR comments
1 parent 7aad8c6 commit a983537

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace MLAPI
55
{
66
public class NetworkTickSystem : INetworkUpdateSystem, IDisposable
77
{
8-
private const double k_DefaultTickDuration = 0.016;
9-
private double m_TickDuration;
10-
private int m_NetworkTick; //How many network ticks have passed?
8+
private const float k_DefaultTickDuration = 1/60f; // Default to 60 FPS
9+
private float m_TickInterval; //Duration of a tick
10+
private int m_NetworkTickCount; //How many network ticks have passed?
1111

1212
private static NetworkTickSystem m_Instance = null;
1313

@@ -28,9 +28,25 @@ public static NetworkTickSystem Instance
2828
}
2929
}
3030

31+
/// <summary>
32+
/// Constructor
33+
/// Defaults to k_DefaultTickDuration if no tick duration is specified
34+
/// </summary>
35+
/// <param name="tickInterval">Duration of a network tick</param>
36+
private NetworkTickSystem(float tickInterval = k_DefaultTickDuration)
37+
{
38+
this.RegisterNetworkUpdate(NetworkUpdateStage.EarlyUpdate);
39+
40+
//Assure we don't specify a value less than or equal to zero for tick frequency
41+
m_TickInterval = (tickInterval <= 0f) ? k_DefaultTickDuration : tickInterval;
42+
43+
// ticks might not start at 0, so let's update right away at construction
44+
UpdateNetworkTick();
45+
}
46+
3147
public void Dispose()
3248
{
33-
NetworkUpdateLoop.UnregisterNetworkUpdate(this, NetworkUpdateStage.EarlyUpdate);
49+
this.UnregisterNetworkUpdate(NetworkUpdateStage.EarlyUpdate);
3450
}
3551

3652
/// <summary>
@@ -40,17 +56,17 @@ public void Dispose()
4056
/// <returns></returns>
4157
public ushort GetTick()
4258
{
43-
return (ushort)(m_NetworkTick % k_TickPeriod);
59+
return (ushort)(m_NetworkTickCount % k_TickPeriod);
4460
}
4561

4662
/// <summary>
4763
/// GetNetworkTime
48-
/// Network time is calculated from m_NetworkTick and m_TickDuration (tick frequency)
64+
/// Network time is calculated from m_NetworkTickCount and m_TickInterval (tick frequency)
4965
/// </summary>
5066
/// <returns>Network Time</returns>
51-
public double GetNetworkTime()
67+
public float GetNetworkTime()
5268
{
53-
return m_NetworkTick * m_TickDuration;
69+
return m_NetworkTickCount * m_TickInterval;
5470
}
5571

5672
/// <summary>
@@ -59,23 +75,7 @@ public double GetNetworkTime()
5975
/// </summary>
6076
private void UpdateNetworkTick()
6177
{
62-
m_NetworkTick = (int)(Time.unscaledTime / m_TickDuration);
63-
}
64-
65-
/// <summary>
66-
/// Constructor
67-
/// Defaults to k_DefaultTickDuration if no tick duration is specified
68-
/// </summary>
69-
/// <param name="tickDuration">Duration of a network tick</param>
70-
private NetworkTickSystem(double tickDuration = k_DefaultTickDuration)
71-
{
72-
NetworkUpdateLoop.RegisterNetworkUpdate(this, NetworkUpdateStage.EarlyUpdate);
73-
74-
//Assure we don't specify a value less than or equal to zero for tick frequency
75-
m_TickDuration = (tickDuration <= 0d) ? k_DefaultTickDuration : tickDuration;
76-
77-
// ticks might not start at 0, so let's update right away at construction
78-
UpdateNetworkTick();
78+
m_NetworkTickCount = (int)(Time.unscaledTime / m_TickInterval);
7979
}
8080

8181
public void NetworkUpdate(NetworkUpdateStage updateStage)

0 commit comments

Comments
 (0)