Skip to content

Commit 891bd67

Browse files
authored
fix: Fixed Owner-written NetworkVariable infinitely write themselves (#2128)
* fix: Fixed Owner-written NetworkVariable infinitely write themselves (#2109) Fixed by doing the pre-write as a separate step * nit PR review :-) * Adding test
1 parent 2d9f786 commit 891bd67

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

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

1717
### Fixed
1818

19+
- Fixed Owner-written NetworkVariable infinitely write themselves (#2109)
20+
- Fixed NetworkList issue that showed when inserting at the very end of a NetworkList (#2099)
1921
- Fixed issue where a client owner of a `NetworkVariable` with both owner read and write permissions would not update the server side when changed. (#2097)
2022
- Fixed issue when attempting to spawn a parent `GameObject`, with `NetworkObject` component attached, that has one or more child `GameObject`s, that are inactive in the hierarchy, with `NetworkBehaviour` components it will no longer attempt to spawn the associated `NetworkBehaviour`(s) or invoke ownership changed notifications but will log a warning message. (#2096)
2123
- Fixed an issue where destroying a NetworkBehaviour would not deregister it from the parent NetworkObject, leading to exceptions when the parent was later destroyed. (#2091)
@@ -24,9 +26,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
2426
- Fixed issue where `NetworkAnimator` would not synchronize a looping animation for late joining clients if it was at the very end of its loop. (#2076)
2527
- Fixed issue where `NetworkAnimator` was not removing its subscription from `OnClientConnectedCallback` when despawned during the shutdown sequence. (#2074)
2628
- Fixed IsServer and IsClient being set to false before object despawn during the shutdown sequence. (#2074)
27-
- Fixed NetworkLists not populating on client. NetworkList now uses the most recent list as opposed to the list at the end of previous frame, when sending full updates to dynamically spawned NetworkObject. The difference in behaviour is required as scene management spawns those objects at a different time in the frame, relative to updates. (#2062)
2829
- Fixed NetworkList Value event on the server. PreviousValue is now set correctly when a new value is set through property setter. (#2067)
29-
- Fixed NetworkList issue that showed when inserting at the very end of a NetworkList (#2099)
30+
- Fixed NetworkLists not populating on client. NetworkList now uses the most recent list as opposed to the list at the end of previous frame, when sending full updates to dynamically spawned NetworkObject. The difference in behaviour is required as scene management spawns those objects at a different time in the frame, relative to updates. (#2062)
3031

3132
## [1.0.0] - 2022-06-27
3233

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,18 @@ internal void PostNetworkVariableWrite()
587587
MarkVariablesDirty(false);
588588
}
589589

590-
internal void VariableUpdate(ulong targetClientId)
590+
internal void PreVariableUpdate()
591591
{
592592
if (!m_VarInit)
593593
{
594594
InitializeVariables();
595595
}
596596

597597
PreNetworkVariableWrite();
598+
}
599+
600+
internal void VariableUpdate(ulong targetClientId)
601+
{
598602
NetworkVariableUpdate(targetClientId, NetworkBehaviourId);
599603
}
600604

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ internal void NetworkBehaviourUpdate(NetworkManager networkManager)
3030
{
3131
foreach (var dirtyObj in m_DirtyNetworkObjects)
3232
{
33+
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
34+
{
35+
dirtyObj.ChildNetworkBehaviours[k].PreVariableUpdate();
36+
}
37+
3338
for (int i = 0; i < networkManager.ConnectedClientsList.Count; i++)
3439
{
3540
var client = networkManager.ConnectedClientsList[i];
@@ -56,6 +61,10 @@ internal void NetworkBehaviourUpdate(NetworkManager networkManager)
5661
{
5762
if (sobj.IsOwner)
5863
{
64+
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
65+
{
66+
sobj.ChildNetworkBehaviours[k].PreVariableUpdate();
67+
}
5968
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
6069
{
6170
sobj.ChildNetworkBehaviours[k].VariableUpdate(NetworkManager.ServerClientId);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using UnityEngine;
5+
using UnityEngine.TestTools;
6+
using Unity.Netcode.TestHelpers.Runtime;
7+
8+
namespace Unity.Netcode.RuntimeTests
9+
{
10+
// This is a bit of a quirky test.
11+
// Addresses MTT-4386 #2109
12+
// Where the NetworkVariable updates would be repeated on some clients.
13+
// The twist comes fom the updates needing to happens very specifically for the issue to repro in tests
14+
15+
public class OwnerModifiedObject : NetworkBehaviour, INetworkUpdateSystem
16+
{
17+
public NetworkList<int> MyNetworkList;
18+
19+
static internal int Updates = 0;
20+
21+
private void Awake()
22+
{
23+
MyNetworkList = new NetworkList<int>(new List<int>(), NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
24+
MyNetworkList.OnListChanged += Changed;
25+
}
26+
27+
public void Changed(NetworkListEvent<int> listEvent)
28+
{
29+
var expected = 0;
30+
var listString = "";
31+
foreach (var i in MyNetworkList)
32+
{
33+
Assert.AreEqual(i, expected);
34+
expected++;
35+
listString += i.ToString();
36+
}
37+
Debug.Log($"[{NetworkManager.LocalClientId}] Value changed to {listString}");
38+
Updates++;
39+
}
40+
41+
public bool AddValues;
42+
43+
public NetworkUpdateStage NetworkUpdateStageToCheck;
44+
45+
private int m_ValueToUpdate;
46+
47+
public void NetworkUpdate(NetworkUpdateStage updateStage)
48+
{
49+
if (updateStage == NetworkUpdateStageToCheck)
50+
{
51+
if (AddValues)
52+
{
53+
MyNetworkList.Add(m_ValueToUpdate++);
54+
AddValues = false;
55+
}
56+
}
57+
}
58+
59+
public override void OnDestroy()
60+
{
61+
NetworkUpdateLoop.UnregisterAllNetworkUpdates(this);
62+
base.OnDestroy();
63+
}
64+
65+
public void InitializeLastCient()
66+
{
67+
NetworkUpdateLoop.RegisterAllNetworkUpdates(this);
68+
}
69+
}
70+
71+
public class OwnerModifiedTests : NetcodeIntegrationTest
72+
{
73+
protected override int NumberOfClients => 2;
74+
75+
protected override void OnCreatePlayerPrefab()
76+
{
77+
m_PlayerPrefab.AddComponent<OwnerModifiedObject>();
78+
}
79+
80+
[UnityTest]
81+
public IEnumerator OwnerModifiedTest()
82+
{
83+
// We use this to assure we are the "last client" connected.
84+
yield return CreateAndStartNewClient();
85+
var ownerModLastClient = m_ClientNetworkManagers[2].LocalClient.PlayerObject.GetComponent<OwnerModifiedObject>();
86+
ownerModLastClient.InitializeLastCient();
87+
88+
// Run through all update loops setting the value once every 5 frames
89+
foreach (var updateLoopType in System.Enum.GetValues(typeof(NetworkUpdateStage)))
90+
{
91+
ownerModLastClient.NetworkUpdateStageToCheck = (NetworkUpdateStage)updateLoopType;
92+
Debug.Log($"Testing Update Stage: {ownerModLastClient.NetworkUpdateStageToCheck}");
93+
ownerModLastClient.AddValues = true;
94+
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
95+
}
96+
97+
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
98+
99+
// We'll have at least one update per stage per client, if all goes well.
100+
Assert.True(OwnerModifiedObject.Updates > 20);
101+
}
102+
}
103+
}

com.unity.netcode.gameobjects/Tests/Runtime/OwnerModifiedTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)