Skip to content

fix: AnticipatedNetworkVariable not updating previous value [MTTB-1036] (back port) #3322

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
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
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue where `AnticipatedNetworkVariable` previous value returned by `AnticipatedNetworkVariable.OnAuthoritativeValueChanged` is updated correctly on the non-authoritative side. (#3322)

### Changed


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ public override void ReadField(FastBufferReader reader)
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
m_AuthoritativeValue.ReadDelta(reader, keepDirtyDelta);
// Assure that the post delta read is invoked in order to update
// previous value.
m_AuthoritativeValue.PostDeltaRead();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,5 +416,38 @@ public void WhenStaleDataArrivesToReanticipatedVariable_ItIsAppliedAndReanticipa
Assert.AreEqual(25, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value);
Assert.AreEqual(20, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
}

private int m_PreviousSnapValue;
/// <summary>
/// Validates the previous value is being properly updated on the non-authoritative side.
/// </summary>
[Test]
public void PreviousValueIsMaintainedProperly()
{
var testComponent = GetTestComponent();

testComponent.SnapOnAnticipationFailVariable.OnAuthoritativeValueChanged += OnAuthoritativeValueChanged;
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
testComponent.SetSnapValueRpc(10);
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
// Verify the previous value is 0
Assert.AreEqual(0, m_PreviousSnapValue);
testComponent.SetSnapValueRpc(20);

WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
// Verify the previous value is 10
Assert.AreEqual(10, m_PreviousSnapValue);

testComponent.SetSnapValueRpc(30);
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
// Verify the previous value is 20
Assert.AreEqual(20, m_PreviousSnapValue);
}

private void OnAuthoritativeValueChanged(AnticipatedNetworkVariable<int> anticipatedValue, in int previous, in int current)
{
m_PreviousSnapValue = previous;
}

}
}