Skip to content

Commit cbe3d71

Browse files
update
Minor code clean up with a handful of additional comments.
1 parent 11f49ff commit cbe3d71

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

com.unity.netcode.gameobjects/Editor/NetworkTransformEditor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ private void DisplayNetworkTransformProperties()
194194
}
195195
else
196196
{
197+
// Should only be visible when SwitchTransformSpaceWhenParented is disabled.
197198
EditorGUILayout.PropertyField(m_InLocalSpaceProperty);
198199
}
199200
if (!networkTransform.HideInterpolateValue)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ internal void __endSendRpc(ref FastBufferWriter bufferWriter, uint rpcMethodId,
365365
{
366366
throw new OverflowException("RPC parameters are too large for unreliable delivery.");
367367
}
368-
networkDelivery = NetworkDelivery.UnreliableSequenced;
368+
networkDelivery = NetworkDelivery.Unreliable;
369369
break;
370370
}
371371

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,43 @@ public class NetworkBehaviourUpdater
1111
{
1212
private NetworkManager m_NetworkManager;
1313
private NetworkConnectionManager m_ConnectionManager;
14+
15+
/// <summary>
16+
/// Contains the current dirty <see cref="NetworkObject"/>s that are proccessed each new network tick.
17+
/// Under most cases, dirty <see cref="NetworkObject"/>s are fully processed on the next network tick.
18+
/// Under certain conditions, like user script invoking <see cref="NetworkVariableBase.SetUpdateTraits(NetworkVariableUpdateTraits)"/>
19+
/// to define <see cref="NetworkVariableUpdateTraits"/>, a <see cref="NetworkObject"/> can remain in the
20+
/// <see cref="m_DirtyNetworkObjects"/> list until the configured traits' conditions have been met.
21+
/// </summary>
1422
private HashSet<NetworkObject> m_DirtyNetworkObjects = new HashSet<NetworkObject>();
23+
24+
/// <summary>
25+
/// Contains any dirty <see cref="NetworkObject"/>s that will be added to the <see cref="m_DirtyNetworkObjects"/>
26+
/// list on the next network tick (<see cref="OnNetworkTick"/>).
27+
/// </summary>
1528
private HashSet<NetworkObject> m_PendingDirtyNetworkObjects = new HashSet<NetworkObject>();
1629

1730
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1831
private ProfilerMarker m_NetworkBehaviourUpdate = new ProfilerMarker($"{nameof(NetworkBehaviour)}.{nameof(NetworkBehaviourUpdate)}");
1932
#endif
2033

34+
/// <summary>
35+
/// Adds a <see cref="NetworkObject"/> to the prending dirty list.
36+
/// The <see cref="m_PendingDirtyNetworkObjects"/> list is merged into the <see cref="m_DirtyNetworkObjects"/> list
37+
/// when processed.
38+
/// </summary>
2139
internal void AddForUpdate(NetworkObject networkObject)
2240
{
2341
// Since this is a HashSet, we don't need to worry about duplicate entries
2442
m_PendingDirtyNetworkObjects.Add(networkObject);
2543
}
2644

45+
/// <summary>
46+
/// (Client-server network topology only)
47+
/// The server handles processing network variables the same way as a client
48+
/// with the primary difference being that the server sends updates to all
49+
/// observers.
50+
/// </summary>
2751
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2852
internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
2953
{
@@ -41,6 +65,12 @@ internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
4165
}
4266
}
4367

68+
/// <summary>
69+
/// Clients handle processing dirty objects relative to the client.
70+
/// The <see cref="NetworkVariableDeltaMessage"/> is client to server.
71+
/// With distributed authority live service sessions, this is sent to
72+
/// the Rust server.
73+
/// </summary>
4474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4575
internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
4676
{
@@ -50,6 +80,10 @@ internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
5080
}
5181
}
5282

83+
/// <summary>
84+
/// Handle house cleaning on the child <see cref="NetworkBehaviour"/>s.
85+
/// This includes some collections specific checks and updates.
86+
/// </summary>
5387
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5488
internal void PostProcessDirtyObject(NetworkObject dirtyObj)
5589
{
@@ -73,6 +107,11 @@ internal void PostProcessDirtyObject(NetworkObject dirtyObj)
73107
}
74108
}
75109

110+
/// <summary>
111+
/// Invokes <see cref="NetworkBehaviour.PostNetworkVariableWrite(bool)"/> on all child <see cref="NetworkBehaviour"/>s.
112+
/// </summary>
113+
/// <param name="dirtyObj"></param>
114+
/// <param name="forceSend">Refer to the <see cref="ProcessDirtyObject(NetworkObject, bool)"/> definition.</param>
76115
[MethodImpl(MethodImplOptions.AggressiveInlining)]
77116
internal void ResetDirtyObject(NetworkObject dirtyObj, bool forceSend)
78117
{
@@ -103,6 +142,18 @@ internal void ForceSendIfDirtyOnNetworkShow(NetworkObject networkObject)
103142
m_DirtyNetworkObjects.Remove(networkObject);
104143
}
105144

145+
/// <summary>
146+
/// The primary "dirty" <see cref="NetworkObject"/> processor.
147+
/// Invokes:
148+
/// - <see cref="NetworkBehaviour.PreVariableUpdate"/> on all properties that derive from <see cref="NetworkVariableBase"/>.
149+
/// - <see cref="ProcessDirtyObjectServer(NetworkObject, bool)"/> (if the server).
150+
/// - <see cref="ProcessDirtyObjectClient(NetworkObject, bool)"/> (if the client).
151+
/// - <see cref="PostProcessDirtyObject"/> to handle the post processing of network variables.
152+
/// - <see cref="ResetDirtyObject"/> which cleans up and removes the <see cref="NetworkObject"/> from the dirty list.
153+
/// </summary>
154+
/// <param name="networkObject">The <see cref="NetworkObject"/> to process.</param>
155+
/// <param name="forceSend">When enabled, any dirty network variables will be added to a
156+
/// <see cref="NetworkVariableDeltaMessage"/> and added to the outbound queue.</param>
106157
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107158
internal void ProcessDirtyObject(NetworkObject networkObject, bool forceSend)
108159
{
@@ -138,7 +189,7 @@ internal void ProcessDirtyObject(NetworkObject networkObject, bool forceSend)
138189
/// <summary>
139190
/// Sends NetworkVariable deltas
140191
/// </summary>
141-
/// <param name="forceSend">internal only, when changing ownership we want to send this before the change in ownership message</param>
192+
/// <param name="forceSend"> Refer to the <see cref="ProcessDirtyObject(NetworkObject, bool)"/> definition.</param>
142193
internal void NetworkBehaviourUpdate(bool forceSend = false)
143194
{
144195
#if DEVELOPMENT_BUILD || UNITY_EDITOR
@@ -175,18 +226,20 @@ internal void Initialize(NetworkManager networkManager)
175226
{
176227
m_NetworkManager = networkManager;
177228
m_ConnectionManager = networkManager.ConnectionManager;
178-
m_NetworkManager.NetworkTickSystem.Tick += NetworkBehaviourUpdater_Tick;
229+
m_NetworkManager.NetworkTickSystem.Tick += OnNetworkTick;
179230
}
180231

181232
internal void Shutdown()
182233
{
183-
m_NetworkManager.NetworkTickSystem.Tick -= NetworkBehaviourUpdater_Tick;
234+
m_NetworkManager.NetworkTickSystem.Tick -= OnNetworkTick;
184235
}
185236

186-
// Order of operations requires NetworkVariable updates first then showing NetworkObjects
187-
private void NetworkBehaviourUpdater_Tick()
237+
/// <summary>
238+
/// Process any dirty <see cref="NetworkObject"/>s on each new
239+
/// network tick.
240+
/// </summary>
241+
private void OnNetworkTick()
188242
{
189-
// Handle showing NetworkObjects on the next network tick, and only if we sent
190243
NetworkBehaviourUpdate();
191244
}
192245
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,15 @@ internal void ShutdownInternal()
15501550
{
15511551
NetworkLog.LogInfo(nameof(ShutdownInternal));
15521552
}
1553+
1554+
// Always wrap events that can invoke user script in a
1555+
// try-catch to assure any proceeding script is still
1556+
// executed.
1557+
// Example:
1558+
// In editor some script registered to OnPreShutdown
1559+
// throws and exception. The UnregisterAllNetworkUpdates
1560+
// will never be invoked which means it will continue to
1561+
// be invoked outside of play mode.
15531562
try
15541563
{
15551564
OnPreShutdown?.Invoke();
@@ -1647,6 +1656,8 @@ internal void ShutdownInternal()
16471656
// Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when quitting the application.
16481657
private void OnApplicationQuit()
16491658
{
1659+
// Abrupt shutdown (or immediate exit of play mode).
1660+
// Assure we unregister from network updates.
16501661
this.UnregisterAllNetworkUpdates();
16511662

16521663
// Make sure ShutdownInProgress returns true during this time
@@ -1679,6 +1690,8 @@ private void OnDestroy()
16791690

16801691
UnityEngine.SceneManagement.SceneManager.sceneUnloaded -= OnSceneUnloaded;
16811692

1693+
// try-catch to assure we reset the Singleton and, if in the editor,
1694+
// unscubscribe from playModeStateChanged.
16821695
try
16831696
{
16841697
// Notify we are destroying NetworkManager

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,8 @@ private void OnDestroy()
17381738
if (!NetworkManager.ShutdownInProgress)
17391739
{
17401740
// Since we still have a session connection, log locally and on the server to inform user of this issue.
1741+
// If the NetworkObject's GaaeObject is not valid or the scene is no longer valid or loaded, then this was due to the
1742+
// unloading of a scene which is done by the authority...
17411743
if (gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded)
17421744
{
17431745
if (NetworkManager.LogLevel <= LogLevel.Error && gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded)
@@ -1755,6 +1757,7 @@ private void OnDestroy()
17551757
}
17561758
else
17571759
{
1760+
// If the destroy was authority scene event triggered, then mark this destroy as authority triggered.
17581761
isAuthorityDestroy = true;
17591762
}
17601763
}

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ internal NetworkObject CreateLocalNetworkObject(NetworkObject.SceneObject sceneO
10291029
}
10301030

10311031
/// <summary>
1032+
/// Only spawn authority <see cref="NetworkManager"/> instances should invoke this.
10321033
/// Invoked from:
10331034
/// - ConnectionManager after instantiating a player prefab when running in client-server.
10341035
/// - NetworkObject when spawning a newly instantiated NetworkObject for the first time.
@@ -1114,8 +1115,9 @@ internal void AuthorityLocalSpawn(NetworkObject networkObject, ulong networkId,
11141115
}
11151116

11161117
/// <summary>
1117-
/// This is only invoked to instantiate a serialized NetworkObject via
1118-
/// <see cref="NetworkObject.AddSceneObject(in NetworkObject.SceneObject, FastBufferReader, NetworkManager, bool)"/>
1118+
/// Only spawn non-authority <see cref="NetworkManager"/> instances should invoke this.
1119+
/// This is invoked to instantiate an authority spawned <see cref="NetworkObject"/>, and
1120+
/// is only invoked by: <see cref="NetworkObject.AddSceneObject(in NetworkObject.SceneObject, FastBufferReader, NetworkManager, bool)"/>
11191121
/// </summary>
11201122
/// <remarks>
11211123
/// IMPORTANT: Pre spawn methods need to be invoked from within <see cref="NetworkObject.AddSceneObject"/>.
@@ -1783,8 +1785,7 @@ internal void UpdateObservedNetworkObjects(ulong clientId)
17831785
}
17841786

17851787
/// <summary>
1786-
/// See <see cref="NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick"/>
1787-
/// See <see cref="NetworkManager.NetworkUpdate(NetworkUpdateStage)"/> during the <see cref="NetworkUpdateStage.PostLateUpdate"/> stage.
1788+
/// This is only invoked by <see cref="NetworkManager.NetworkUpdate(NetworkUpdateStage)"/> during the <see cref="NetworkUpdateStage.PostLateUpdate"/> stage.
17881789
/// </summary>
17891790
internal void HandleNetworkObjectShow(bool forceSend = false)
17901791
{

0 commit comments

Comments
 (0)