Skip to content

Commit

Permalink
fix: adding reset function to InterpolationTime to avoid error messag…
Browse files Browse the repository at this point in the history
…e on 2nd start

if starting a second time _latestServerTime would still be previous value, this would cause error message.
This likely fixes any issues with _clientTime being far ahead if joining a different server a second time.
  • Loading branch information
James-Frowen committed Apr 11, 2022
1 parent ab623a3 commit 646673d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion source/InterpolationTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ public void OnUpdate(float deltaTime)
/// <param name="serverTime"></param>
public void OnMessage(float serverTime)
{
logger.Assert(serverTime > _latestServerTime, $"Received message out of order. Server Time: {serverTime} vs New Time: {_latestServerTime}");
// only check this if we are intialized
if (intialized)
logger.Assert(serverTime > _latestServerTime, $"Received message out of order. Server Time: {serverTime} vs New Time: {_latestServerTime}");

_latestServerTime = serverTime;

// If this is the first message, set the client time to the server difference.
Expand Down Expand Up @@ -198,6 +201,16 @@ public void OnMessage(float serverTime)
if (logger.LogEnabled()) logger.Log($"st: {serverTime:0.00}, ct: {_clientTime:0.00}, diff: {diff * 1000:0.0}, wanted: {diffAvg.Value * 1000:0.0}, scale: {clientScaleTime}");
}

/// <summary>
/// Call this when start new client to reset timer
/// </summary>
public void Reset()
{
// mark this so first server method will call InitNew
intialized = false;
_latestServerTime = 0;
}

/// <summary>
/// Initializes and resets the system.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions source/SyncPositionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ private void ClientStarted()
if (!ServerActive)
timer = new Timer();

// reset time incase this is 2nd time client starts
_timeSync.Reset();
Client.MessageHandler.RegisterHandler<NetworkPositionMessage>(ClientHandleNetworkPositionMessage);
}

Expand Down

0 comments on commit 646673d

Please sign in to comment.