Skip to content

fix: network time system tests should use double fixed delta time #3373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public struct NetworkTime
/// </summary>
public float FixedDeltaTime => (float)m_TickInterval;

/// <summary>
/// Gets the fixed delta time as a double. This value is calculated by dividing 1.0 by the <see cref="TickRate"/> and stays constant.
/// </summary>
public double FixedDeltaTimeAsDouble => m_TickInterval;

/// <summary>
/// Gets the amount of network ticks which have passed until reaching the current time value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,34 @@ public IEnumerator PlayerLoopTimeTest_WithDifferentTimeScale([Values(0.0f, 0.1f,
public IEnumerator CorrectAmountTicksTest()
{
NetworkTickSystem tickSystem = NetworkManager.Singleton.NetworkTickSystem;
float delta = tickSystem.LocalTime.FixedDeltaTime;
double delta = tickSystem.LocalTime.FixedDeltaTimeAsDouble;
int previous_localTickCalculated = 0;
int previous_serverTickCalculated = 0;

while (tickSystem.LocalTime.Time < 3f)
{
yield return null;

var tickCalculated = tickSystem.LocalTime.Time / delta;
previous_localTickCalculated = (int)tickCalculated;
var localTickCalculated = tickSystem.LocalTime.Time / delta;
previous_localTickCalculated = (int)localTickCalculated;

// This check is needed due to double division imprecision of large numbers
if ((tickCalculated - previous_localTickCalculated) >= 0.999999999999)
if ((localTickCalculated - previous_localTickCalculated) >= 0.999999999999)
{
previous_localTickCalculated++;
}


tickCalculated = NetworkManager.Singleton.ServerTime.Time / delta;
previous_serverTickCalculated = (int)tickCalculated;
var serverTickCalculated = tickSystem.ServerTime.Time / delta;
previous_serverTickCalculated = (int)serverTickCalculated;

// This check is needed due to double division imprecision of large numbers
if ((tickCalculated - previous_serverTickCalculated) >= 0.999999999999)
if ((serverTickCalculated - previous_serverTickCalculated) >= 0.999999999999)
{
previous_serverTickCalculated++;
}

Assert.AreEqual(previous_localTickCalculated, NetworkManager.Singleton.LocalTime.Tick, $"Calculated local tick {previous_localTickCalculated} does not match local tick {NetworkManager.Singleton.LocalTime.Tick}!");
Assert.AreEqual(previous_serverTickCalculated, NetworkManager.Singleton.ServerTime.Tick, $"Calculated server tick {previous_serverTickCalculated} does not match server tick {NetworkManager.Singleton.ServerTime.Tick}!");
Assert.AreEqual(previous_localTickCalculated, NetworkManager.Singleton.LocalTime.Tick, $"Calculated local tick {previous_localTickCalculated} does not match local tick {NetworkManager.Singleton.LocalTime.Tick}!]n Local Tick-Calc: {localTickCalculated} LocalTime: {tickSystem.LocalTime.Time} | Server Tick-Calc: {serverTickCalculated} ServerTime: {tickSystem.ServerTime.Time} | TickDelta: {delta}");
Assert.AreEqual(previous_serverTickCalculated, NetworkManager.Singleton.ServerTime.Tick, $"Calculated server tick {previous_serverTickCalculated} does not match server tick {NetworkManager.Singleton.ServerTime.Tick}!\n Local Tick-Calc: {localTickCalculated} LocalTime: {tickSystem.LocalTime.Time} | Server Tick-Calc: {serverTickCalculated} ServerTime: {tickSystem.ServerTime.Time} | TickDelta: {delta}");
Assert.AreEqual((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time, $"Local time {(float)NetworkManager.Singleton.LocalTime.Time} is not approximately server time {(float)NetworkManager.Singleton.ServerTime.Time}!", FloatComparer.s_ComparerWithDefaultTolerance);
}
}
Expand Down