From 5862d9d5b1a6531b5911c167cfb1f65e1d77e175 Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Tue, 18 Feb 2025 18:50:09 -0600 Subject: [PATCH 1/4] fix Fixing AnticipatedNetworkVariable issue where the previous value was not being updated for non-authority/write permission instances. --- .../Runtime/NetworkVariable/AnticipatedNetworkVariable.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs index d9d4e87393..75e533b29d 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs @@ -387,6 +387,7 @@ public override void ReadField(FastBufferReader reader) public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta) { m_AuthoritativeValue.ReadDelta(reader, keepDirtyDelta); + m_AuthoritativeValue.PostDeltaRead(); } } } From 92e027adcee6bb517116e8ba4b262a87afaf1b70 Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Tue, 18 Feb 2025 18:51:21 -0600 Subject: [PATCH 2/4] style adding comment --- .../Runtime/NetworkVariable/AnticipatedNetworkVariable.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs index 75e533b29d..94625722e3 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs @@ -387,6 +387,8 @@ 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(); } } From aa33bc0953deba692b29212d6a2f1b0d62be8ab6 Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Mon, 24 Feb 2025 15:18:45 -0600 Subject: [PATCH 3/4] test Adding a test to validate this fix. --- .../NetworkVariableAnticipationTests.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs index c436275fea..66d2d8d90f 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs @@ -416,5 +416,39 @@ public void WhenStaleDataArrivesToReanticipatedVariable_ItIsAppliedAndReanticipa Assert.AreEqual(25, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value); Assert.AreEqual(20, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue); } + + + private int m_PreviousSnapValue; + /// + /// Validates the previous value is being properly updated on the non-authoritative side. + /// + [Test] + public void PreviousValueIsMaintainedProperly() + { + var testComponent = GetTestComponent(); + + testComponent.SnapOnAnticipationFailVariable.OnAuthoritativeValueChanged += OnAuthoritativeValueChanged; + testComponent.SnapOnAnticipationFailVariable.Anticipate(10); + testComponent.SetSnapValueRpc(10); + WaitForMessageReceivedWithTimeTravel(m_ClientNetworkManagers.ToList()); + // Verify the previous value is 0 + Assert.AreEqual(0, m_PreviousSnapValue); + testComponent.SetSnapValueRpc(20); + + WaitForMessageReceivedWithTimeTravel(m_ClientNetworkManagers.ToList()); + // Verify the previous value is 10 + Assert.AreEqual(10, m_PreviousSnapValue); + + testComponent.SetSnapValueRpc(30); + WaitForMessageReceivedWithTimeTravel(m_ClientNetworkManagers.ToList()); + // Verify the previous value is 20 + Assert.AreEqual(20, m_PreviousSnapValue); + } + + private void OnAuthoritativeValueChanged(AnticipatedNetworkVariable anticipatedValue, in int previous, in int current) + { + m_PreviousSnapValue = previous; + } + } } From 8bcbdd356646c841a568dce9df77c8964acec82b Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Mon, 24 Feb 2025 15:21:13 -0600 Subject: [PATCH 4/4] update adding change log entry. --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 6922d1eeda..5a3da061a1 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -12,6 +12,7 @@ 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. (#3306) - Fixed `OnClientConnectedCallback` passing incorrect `clientId` when scene management is disabled. (#3312) - Fixed issue where the `NetworkObject.Ownership` custom editor did not take the default "Everything" flag into consideration. (#3305) - Fixed DestroyObject flow on non-authority game clients. (#3291)