@@ -57,7 +57,7 @@ public override void OnInitialize()
5757 base . OnInitialize ( ) ;
5858
5959 m_HasPreviousValue = true ;
60- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
60+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
6161 NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_PreviousValue ) ;
6262 }
6363
@@ -73,7 +73,7 @@ public NetworkVariable(T value = default,
7373 : base ( readPerm , writePerm )
7474 {
7575 m_InternalValue = value ;
76- m_InternalOriginalValue = default ;
76+ m_LastInternalValue = default ;
7777 // Since we start with IsDirty = true, this doesn't need to be duplicated
7878 // right away. It won't get read until after ResetDirty() is called, and
7979 // the duplicate will be made there. Avoiding calling
@@ -92,25 +92,45 @@ public void Reset(T value = default)
9292 if ( m_NetworkBehaviour == null || m_NetworkBehaviour != null && ! m_NetworkBehaviour . NetworkObject . IsSpawned )
9393 {
9494 m_InternalValue = value ;
95- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
95+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
9696 m_PreviousValue = default ;
9797 }
9898 }
9999
100100 /// <summary>
101- /// The internal value of the NetworkVariable
101+ /// The current internal value of the NetworkVariable.
102102 /// </summary>
103+ /// <remarks>
104+ /// When using collections, this InternalValue can be updated directly without going through the <see cref="NetworkVariable{T}.Value"/> setter.
105+ /// </remarks>
103106 [ SerializeField ]
104107 private protected T m_InternalValue ;
105108
106- // The introduction of standard .NET collections caused an issue with permissions since there is no way to detect changes in the
107- // collection without doing a full comparison. While this approach does consume more memory per collection instance, it is the
108- // lowest risk approach to resolving the issue where a client with no write permissions could make changes to a collection locally
109- // which can cause a myriad of issues.
110- private protected T m_InternalOriginalValue ;
109+ /// <summary>
110+ /// The last valid/authorized value of the network variable.
111+ /// </summary>
112+ /// <remarks>
113+ /// The introduction of standard .NET collections caused an issue with permissions since there is no way to detect changes in the
114+ /// collection without doing a full comparison. While this approach does consume more memory per collection instance, it is the
115+ /// lowest risk approach to resolving the issue where a client with no write permissions could make changes to a collection locally
116+ /// which can cause a myriad of issues.
117+ /// </remarks>
118+ private protected T m_LastInternalValue ;
111119
120+ /// <summary>
121+ /// The most recent value that was synchronized over the network.
122+ /// Synchronized over the network at the end of the frame in which the <see cref="NetworkVariable{T}"/> was marked dirty.
123+ /// </summary>
124+ /// <remarks>
125+ /// Only contains the value synchronized over the network at the end of the last frame.
126+ /// All in-between changes on the authority are tracked by <see cref="m_LastInternalValue"/>.
127+ /// </remarks>
112128 private protected T m_PreviousValue ;
113129
130+ /// <summary>
131+ /// Whether this network variable has had changes synchronized over the network.
132+ /// Indicates whether <see cref="m_PreviousValue"/> is populated and valid.
133+ /// </summary>
114134 private bool m_HasPreviousValue ;
115135 private bool m_IsDisposed ;
116136
@@ -139,7 +159,7 @@ public virtual T Value
139159 {
140160 T previousValue = m_InternalValue ;
141161 m_InternalValue = value ;
142- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
162+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
143163 SetDirty ( true ) ;
144164 m_IsDisposed = false ;
145165 OnValueChanged ? . Invoke ( previousValue , m_InternalValue ) ;
@@ -165,21 +185,21 @@ public bool CheckDirtyState(bool forceCheck = false)
165185 if ( CannotWrite )
166186 {
167187 // If modifications are detected, then revert back to the last known current value
168- if ( ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalValue , ref m_InternalOriginalValue ) )
188+ if ( ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalValue , ref m_LastInternalValue ) )
169189 {
170- NetworkVariableSerialization < T > . Duplicate ( m_InternalOriginalValue , ref m_InternalValue ) ;
190+ NetworkVariableSerialization < T > . Duplicate ( m_LastInternalValue , ref m_InternalValue ) ;
171191 }
172192 return false ;
173193 }
174194
175- // Compare the previous with the current if not dirty or forcing a check.
176- if ( ( ! isDirty || forceCheck ) && ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalOriginalValue , ref m_InternalValue ) )
195+ // Compare the last internal value with the current value if not dirty or forcing a check.
196+ if ( ( ! isDirty || forceCheck ) && ! NetworkVariableSerialization < T > . AreEqual ( ref m_LastInternalValue , ref m_InternalValue ) )
177197 {
178198 SetDirty ( true ) ;
179- OnValueChanged ? . Invoke ( m_InternalOriginalValue , m_InternalValue ) ;
199+ OnValueChanged ? . Invoke ( m_LastInternalValue , m_InternalValue ) ;
180200 m_IsDisposed = false ;
181201 isDirty = true ;
182- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
202+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
183203 }
184204 return isDirty ;
185205 }
@@ -213,11 +233,11 @@ public override void Dispose()
213233 m_InternalValue = default ;
214234
215235 // Dispose the internal original value
216- if ( m_InternalOriginalValue is IDisposable internalOriginalValueDisposable )
236+ if ( m_LastInternalValue is IDisposable internalOriginalValueDisposable )
217237 {
218238 internalOriginalValueDisposable . Dispose ( ) ;
219239 }
220- m_InternalOriginalValue = default ;
240+ m_LastInternalValue = default ;
221241
222242 // Dispose the previous value if there is one
223243 if ( m_HasPreviousValue && m_PreviousValue is IDisposable previousValueDisposable )
@@ -246,9 +266,9 @@ public override bool IsDirty()
246266 {
247267 // If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
248268 // to the original collection value prior to applying updates (primarily for collections).
249- if ( ! NetworkUpdaterCheck && CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalValue , ref m_InternalOriginalValue ) )
269+ if ( ! NetworkUpdaterCheck && CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalValue , ref m_LastInternalValue ) )
250270 {
251- NetworkVariableSerialization < T > . Duplicate ( m_InternalOriginalValue , ref m_InternalValue ) ;
271+ NetworkVariableSerialization < T > . Duplicate ( m_LastInternalValue , ref m_InternalValue ) ;
252272 return true ;
253273 }
254274 // For most cases we can use the dirty flag.
@@ -285,7 +305,7 @@ public override void ResetDirty()
285305 m_HasPreviousValue = true ;
286306 NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_PreviousValue ) ;
287307 // Once updated, assure the original current value is updated for future comparison purposes
288- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
308+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
289309 }
290310 base . ResetDirty ( ) ;
291311 }
@@ -308,9 +328,9 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
308328 {
309329 // If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
310330 // to the original collection value prior to applying updates (primarily for collections).
311- if ( CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalOriginalValue , ref m_InternalValue ) )
331+ if ( CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_LastInternalValue , ref m_InternalValue ) )
312332 {
313- NetworkVariableSerialization < T > . Duplicate ( m_InternalOriginalValue , ref m_InternalValue ) ;
333+ NetworkVariableSerialization < T > . Duplicate ( m_LastInternalValue , ref m_InternalValue ) ;
314334 }
315335
316336 NetworkVariableSerialization < T > . ReadDelta ( reader , ref m_InternalValue ) ;
@@ -342,17 +362,17 @@ internal override void PostDeltaRead()
342362 m_HasPreviousValue = true ;
343363 NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_PreviousValue ) ;
344364 // Once updated, assure the original current value is updated for future comparison purposes
345- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
365+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
346366 }
347367
348368 /// <inheritdoc />
349369 public override void ReadField ( FastBufferReader reader )
350370 {
351371 // If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
352372 // to the original collection value prior to applying updates (primarily for collections).
353- if ( CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_InternalOriginalValue , ref m_InternalValue ) )
373+ if ( CannotWrite && ! NetworkVariableSerialization < T > . AreEqual ( ref m_LastInternalValue , ref m_InternalValue ) )
354374 {
355- NetworkVariableSerialization < T > . Duplicate ( m_InternalOriginalValue , ref m_InternalValue ) ;
375+ NetworkVariableSerialization < T > . Duplicate ( m_LastInternalValue , ref m_InternalValue ) ;
356376 }
357377
358378 NetworkVariableSerialization < T > . Read ( reader , ref m_InternalValue ) ;
@@ -364,7 +384,7 @@ public override void ReadField(FastBufferReader reader)
364384 NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_PreviousValue ) ;
365385
366386 // Once updated, assure the original current value is updated for future comparison purposes
367- NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_InternalOriginalValue ) ;
387+ NetworkVariableSerialization < T > . Duplicate ( m_InternalValue , ref m_LastInternalValue ) ;
368388 }
369389
370390 /// <inheritdoc />
0 commit comments