Skip to content

Commit 5969784

Browse files
fix: AnticipatedNetworkVariable not updating previous value [MTTB-1036] (back port) (#3322)
Back-port of #3306. This fixes the issue where the `AnticipatedNetworkVariable` previous value was never updated on the non-authority/write permission instances when the `OnAuthoritativeValueChanged` callback was invoked. [MTTB-1036](https://jira.unity3d.com/browse/MTTB-1036) <!-- Add RFC link here if applicable. --> ## Changelog - Fixed: Issue where the `AnticipatedNetworkVariable` previous value was never updated on the non-authority/write permission instances when the `OnAuthoritativeValueChanged` callback was invoked. ## Testing and Documentation - Includes integration test `NetworkVariableAnticipationTests.PreviousValueIsMaintainedProperly`. - No documentation changes or additions were necessary. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. -->
1 parent 3b63bdf commit 5969784

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1414

1515
### Fixed
1616

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

1921

com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ public override void ReadField(FastBufferReader reader)
387387
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
388388
{
389389
m_AuthoritativeValue.ReadDelta(reader, keepDirtyDelta);
390+
// Assure that the post delta read is invoked in order to update
391+
// previous value.
392+
m_AuthoritativeValue.PostDeltaRead();
390393
}
391394
}
392395
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableAnticipationTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,5 +416,38 @@ public void WhenStaleDataArrivesToReanticipatedVariable_ItIsAppliedAndReanticipa
416416
Assert.AreEqual(25, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value);
417417
Assert.AreEqual(20, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
418418
}
419+
420+
private int m_PreviousSnapValue;
421+
/// <summary>
422+
/// Validates the previous value is being properly updated on the non-authoritative side.
423+
/// </summary>
424+
[Test]
425+
public void PreviousValueIsMaintainedProperly()
426+
{
427+
var testComponent = GetTestComponent();
428+
429+
testComponent.SnapOnAnticipationFailVariable.OnAuthoritativeValueChanged += OnAuthoritativeValueChanged;
430+
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
431+
testComponent.SetSnapValueRpc(10);
432+
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
433+
// Verify the previous value is 0
434+
Assert.AreEqual(0, m_PreviousSnapValue);
435+
testComponent.SetSnapValueRpc(20);
436+
437+
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
438+
// Verify the previous value is 10
439+
Assert.AreEqual(10, m_PreviousSnapValue);
440+
441+
testComponent.SetSnapValueRpc(30);
442+
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
443+
// Verify the previous value is 20
444+
Assert.AreEqual(20, m_PreviousSnapValue);
445+
}
446+
447+
private void OnAuthoritativeValueChanged(AnticipatedNetworkVariable<int> anticipatedValue, in int previous, in int current)
448+
{
449+
m_PreviousSnapValue = previous;
450+
}
451+
419452
}
420453
}

0 commit comments

Comments
 (0)