@@ -25,14 +25,8 @@ internal void AddForUpdate(NetworkObject networkObject)
2525 }
2626
2727 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
28- internal bool ProcessDirtyObjectServer ( NetworkObject dirtyObj , bool forceSend )
28+ internal void ProcessDirtyObjectServer ( NetworkObject dirtyObj , bool forceSend )
2929 {
30- var sentMessages = false ;
31- for ( int k = 0 ; k < dirtyObj . ChildNetworkBehaviours . Count ; k ++ )
32- {
33- dirtyObj . ChildNetworkBehaviours [ k ] . PreVariableUpdate ( ) ;
34- }
35-
3630 for ( int i = 0 ; i < m_ConnectionManager . ConnectedClientsList . Count ; i ++ )
3731 {
3832 var client = m_ConnectionManager . ConnectedClientsList [ i ] ;
@@ -42,30 +36,18 @@ internal bool ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
4236 for ( int k = 0 ; k < dirtyObj . ChildNetworkBehaviours . Count ; k ++ )
4337 {
4438 dirtyObj . ChildNetworkBehaviours [ k ] . NetworkVariableUpdate ( client . ClientId , forceSend ) ;
45- sentMessages = true ;
4639 }
4740 }
4841 }
49- return sentMessages ;
5042 }
5143
5244 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
53- internal bool ProcessDirtyObjectClient ( NetworkObject dirtyObj , bool forceSend )
45+ internal void ProcessDirtyObjectClient ( NetworkObject dirtyObj , bool forceSend )
5446 {
55- var sentMessages = false ;
56- if ( dirtyObj . IsOwner )
47+ for ( int k = 0 ; k < dirtyObj . ChildNetworkBehaviours . Count ; k ++ )
5748 {
58- for ( int k = 0 ; k < dirtyObj . ChildNetworkBehaviours . Count ; k ++ )
59- {
60- dirtyObj . ChildNetworkBehaviours [ k ] . PreVariableUpdate ( ) ;
61- }
62- for ( int k = 0 ; k < dirtyObj . ChildNetworkBehaviours . Count ; k ++ )
63- {
64- dirtyObj . ChildNetworkBehaviours [ k ] . NetworkVariableUpdate ( NetworkManager . ServerClientId , forceSend ) ;
65- sentMessages = true ;
66- }
49+ dirtyObj . ChildNetworkBehaviours [ k ] . NetworkVariableUpdate ( NetworkManager . ServerClientId , forceSend ) ;
6750 }
68- return sentMessages ;
6951 }
7052
7153 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -103,45 +85,65 @@ internal void ResetDirtyObject(NetworkObject dirtyObj, bool forceSend)
10385 /// <summary>
10486 /// Temporary work-around for assuring any pending dirty states are pushed out prior to showing the object
10587 /// TODO: We need to send all messages that are specific to a NetworkObject along with a NetworkObject event header
106- /// such that messages will be processed after spawned.
88+ /// and grouped together such that all directed messages will be processed after spawned.
10789 /// </summary>
10890 /// <param name="networkObject"></param>
10991 internal void ForceSendIfDirtyOnNetworkShow ( NetworkObject networkObject )
11092 {
11193 // Exit early if no pending dirty NetworkVariables.
112- if ( ! m_PendingDirtyNetworkObjects . Contains ( networkObject ) )
94+ if ( ! m_PendingDirtyNetworkObjects . Contains ( networkObject ) && ! m_DirtyNetworkObjects . Contains ( networkObject ) )
95+ {
96+ return ;
97+ }
98+
99+ ProcessDirtyObject ( networkObject , true ) ;
100+
101+ // Remove it from the pending and queued dirty objects lists
102+ m_PendingDirtyNetworkObjects . Remove ( networkObject ) ;
103+ m_DirtyNetworkObjects . Remove ( networkObject ) ;
104+ }
105+
106+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
107+ internal void ProcessDirtyObject ( NetworkObject networkObject , bool forceSend )
108+ {
109+ // Only the server or the owner of the NetworkObject will send
110+ // delta state updates. Otherwise, exit early.
111+ if ( ! m_NetworkManager . IsServer || ! networkObject . IsOwner )
113112 {
114113 return ;
115114 }
116115
117- // Process a bit differently whether client or server
116+ // Pre-variable update
117+ for ( int k = 0 ; k < networkObject . ChildNetworkBehaviours . Count ; k ++ )
118+ {
119+ networkObject . ChildNetworkBehaviours [ k ] . PreVariableUpdate ( ) ;
120+ }
121+
122+ // Server sends updates to all clients where a client sends updates
123+ // to the server or DA service.
118124 if ( m_NetworkManager . IsServer )
119125 {
120- ProcessDirtyObjectServer ( networkObject , true ) ;
126+ ProcessDirtyObjectServer ( networkObject , forceSend ) ;
121127 }
122128 else
123129 {
124- ProcessDirtyObjectClient ( networkObject , true ) ;
130+ ProcessDirtyObjectClient ( networkObject , forceSend ) ;
125131 }
126132
127133 // Handle post processing and resetting of the NetworkObject
128134 PostProcessDirtyObject ( networkObject ) ;
129- ResetDirtyObject ( networkObject , true ) ;
130-
131- // Remove it from the pending dirty objects list
132- m_PendingDirtyNetworkObjects . Remove ( networkObject ) ;
135+ ResetDirtyObject ( networkObject , forceSend ) ;
133136 }
134137
135138 /// <summary>
136139 /// Sends NetworkVariable deltas
137140 /// </summary>
138141 /// <param name="forceSend">internal only, when changing ownership we want to send this before the change in ownership message</param>
139- internal bool NetworkBehaviourUpdate ( bool forceSend = false )
142+ internal void NetworkBehaviourUpdate ( bool forceSend = false )
140143 {
141144#if DEVELOPMENT_BUILD || UNITY_EDITOR
142145 m_NetworkBehaviourUpdate . Begin ( ) ;
143146#endif
144- var sentMessages = false ;
145147 try
146148 {
147149 foreach ( var dirtyNetworkObject in m_PendingDirtyNetworkObjects )
@@ -154,31 +156,9 @@ internal bool NetworkBehaviourUpdate(bool forceSend = false)
154156 // trying to process them, even if they were previously marked as dirty.
155157 m_DirtyNetworkObjects . RemoveWhere ( ( sobj ) => sobj == null ) ;
156158
157- if ( m_ConnectionManager . LocalClient . IsServer )
158- {
159- foreach ( var dirtyObj in m_DirtyNetworkObjects )
160- {
161- sentMessages = sentMessages || ProcessDirtyObjectServer ( dirtyObj , forceSend ) ;
162- }
163- }
164- else
165- {
166- // when client updates the server, it tells it about all its objects
167- foreach ( var dirtyObj in m_DirtyNetworkObjects )
168- {
169- sentMessages = sentMessages || ProcessDirtyObjectClient ( dirtyObj , forceSend ) ;
170- }
171- }
172-
173- foreach ( var dirtyObj in m_DirtyNetworkObjects )
174- {
175- PostProcessDirtyObject ( dirtyObj ) ;
176- }
177-
178- // Now, reset all the no-longer-dirty variables
179159 foreach ( var dirtyObj in m_DirtyNetworkObjects )
180160 {
181- ResetDirtyObject ( dirtyObj , forceSend ) ;
161+ ProcessDirtyObject ( dirtyObj , forceSend ) ;
182162 }
183163
184164 m_DirtyNetworkObjects . Clear ( ) ;
@@ -189,7 +169,6 @@ internal bool NetworkBehaviourUpdate(bool forceSend = false)
189169 m_NetworkBehaviourUpdate . End ( ) ;
190170#endif
191171 }
192- return sentMessages ;
193172 }
194173
195174 internal void Initialize ( NetworkManager networkManager )
@@ -207,15 +186,8 @@ internal void Shutdown()
207186 // Order of operations requires NetworkVariable updates first then showing NetworkObjects
208187 private void NetworkBehaviourUpdater_Tick ( )
209188 {
210- // Handle showing NetworkObjects on the next network tick
211- if ( NetworkBehaviourUpdate ( ) )
212- {
213- // Then show any NetworkObjects queued to be made visible/shown
214- m_NetworkManager . SpawnManager . HandleNetworkObjectShow ( ) ;
215- }
216-
217- // Handle object redistribution (DA + disabled scene management only)
218- m_NetworkManager . HandleRedistributionToClients ( ) ;
189+ // Handle showing NetworkObjects on the next network tick, and only if we sent
190+ NetworkBehaviourUpdate ( ) ;
219191 }
220192 }
221193}
0 commit comments