Skip to content

Commit 5e26650

Browse files
authored
fix: prevent Owner-writable NetworkVariable from being sent to the owning client by the server and getting overwritten. NCCBUG-168 (#1871)
1 parent b5f4195 commit 5e26650

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public void Serialize(FastBufferWriter writer)
5454
networkVariable.CanClientRead(TargetClientId) &&
5555
(NetworkBehaviour.NetworkManager.IsServer || networkVariable.CanClientWrite(NetworkBehaviour.NetworkManager.LocalClientId));
5656

57+
// Prevent the server from writing to the client that owns a given NetworkVariable
58+
// Allowing the write would send an old value to the client and cause jitter
59+
if (networkVariable.WritePerm == NetworkVariableWritePermission.Owner &&
60+
networkVariable.OwnerClientId() == TargetClientId)
61+
{
62+
shouldWrite = false;
63+
}
64+
5765
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
5866
{
5967
if (!shouldWrite)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public override void WriteDelta(FastBufferWriter writer)
143143
/// <param name="keepDirtyDelta">Whether or not the container should keep the dirty delta, or mark the delta as consumed</param>
144144
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
145145
{
146+
// todo:
147+
// keepDirtyDelta marks a variable received as dirty and causes the server to send the value to clients
148+
// In a prefect world, whether a variable was A) modified locally or B) received and needs retransmit
149+
// would be stored in different fields
150+
146151
T previousValue = m_InternalValue;
147152
Read(reader, out m_InternalValue);
148153

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public bool CanClientWrite(ulong clientId)
9494
}
9595
}
9696

97+
/// <summary>
98+
/// Returns the ClientId of the owning client
99+
/// </summary>
100+
internal ulong OwnerClientId()
101+
{
102+
return m_NetworkBehaviour.NetworkObject.OwnerClientId;
103+
}
104+
97105
/// <summary>
98106
/// Writes the dirty changes, that is, the changes since the variable was last dirty, to the writer
99107
/// </summary>

0 commit comments

Comments
 (0)