Skip to content

Commit 096c22c

Browse files
fix: AnticipatedNetworkVariable not updating previous value [MTTB-1036] (#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) Back port: #3322 ## 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 1ac59f0 commit 096c22c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed issue where `AnticipatedNetworkVariable` previous value returned by `AnticipatedNetworkVariable.OnAuthoritativeValueChanged` is updated correctly on the non-authoritative side. (#3306)
1819
- Fixed `OnClientConnectedCallback` passing incorrect `clientId` when scene management is disabled. (#3312)
1920
- Fixed issue where the `NetworkObject.Ownership` custom editor did not take the default "Everything" flag into consideration. (#3305)
2021
- Fixed DestroyObject flow on non-authority game clients. (#3291)

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/NetworkVariable/NetworkVariableAnticipationTests.cs

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

0 commit comments

Comments
 (0)