Skip to content

Commit 862c812

Browse files
fix: rounding issue with tick calculation - release 1.0.0 backport (#1617)
* fix Applying the #1604 fix for this release/1.0.0 backport. * test adding the unit test to verify the fix * update updating the changelog to reflect the PR 1614.
1 parent b63328f commit 862c812

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
3535
- Fixed KeyNotFound exception when removing ownership of a newly spawned NetworkObject that is already owned by the server. (#1500)
3636
- Fixed NetworkManager.LocalClient not being set when starting as a host. (#1511)
3737
- Fixed a few memory leak cases when shutting down NetworkManager during Incoming Message Queue processing. (#1323)
38+
- Fixed network tick value sometimes being duplicated or skipped. (#1614)
3839

3940
### Changed
4041
- The SDK no longer limits message size to 64k. (The transport may still impose its own limits, but the SDK no longer does.) (#1384)

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ private void UpdateCache()
114114
{
115115
double d = m_TimeSec / m_TickInterval;
116116
m_CachedTick = (int)d;
117+
// This check is needed due to double division imprecision of large numbers
118+
if ((d - m_CachedTick) >= 0.999999999999)
119+
{
120+
m_CachedTick++;
121+
}
117122
m_CachedTickOffset = ((d - Math.Truncate(d)) * m_TickInterval);
118123

119124
// This handles negative time, decreases tick by 1 and makes offset positive.

com.unity.netcode.gameobjects/Tests/Editor/Timing/NetworkTimeTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,39 @@ private void NetworkTimeAdvanceTestInternal(IEnumerable<float> steps, uint tickR
198198
Assert.IsTrue(Approximately(startTime.Time, (startTime2 - dif).Time, maxAcceptableTotalOffset));
199199
}
200200

201+
[Test]
202+
public void NetworkTickAdvanceTest()
203+
{
204+
var shortSteps = Enumerable.Repeat(1 / 30f, 1000);
205+
NetworkTickAdvanceTestInternal(shortSteps, 30, 0.0f, 0.0f);
206+
}
207+
208+
private NetworkTickSystem m_TickSystem;
209+
private NetworkTimeSystem m_TimeSystem;
210+
private int m_PreviousTick;
211+
212+
private void NetworkTickAdvanceTestInternal(IEnumerable<float> steps, uint tickRate, float start, float start2 = 0f)
213+
{
214+
m_PreviousTick = 0;
215+
m_TickSystem = new NetworkTickSystem(tickRate, start, start2);
216+
m_TimeSystem = NetworkTimeSystem.ServerTimeSystem();
217+
218+
m_TickSystem.Tick += TickUpdate;
219+
foreach (var step in steps)
220+
{
221+
m_TimeSystem.Advance(step);
222+
m_TickSystem.UpdateTick(m_TimeSystem.LocalTime, m_TimeSystem.ServerTime);
223+
}
224+
}
225+
226+
private void TickUpdate()
227+
{
228+
// Make sure our tick is precisely 1 + m_PreviousTick
229+
Assert.IsTrue(m_TickSystem.LocalTime.Tick == m_PreviousTick + 1);
230+
// Assign the m_PreviousTick value for next tick check
231+
m_PreviousTick = m_TickSystem.LocalTime.Tick;
232+
}
233+
201234
private static bool Approximately(double a, double b, double epsilon = 0.000001d)
202235
{
203236
var dif = Math.Abs(a - b);

0 commit comments

Comments
 (0)