@@ -235,42 +235,42 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
235
235
public NetworkManager NetworkManager => NetworkObject . NetworkManager ;
236
236
237
237
/// <summary>
238
- /// Gets if the object is the the personal clients player object
238
+ /// If a NetworkObject is assigned, it will return whether or not this NetworkObject
239
+ /// is the local player object. If no NetworkObject is assigned it will always return false.
239
240
/// </summary>
240
- public bool IsLocalPlayer => NetworkObject . IsLocalPlayer ;
241
+ public bool IsLocalPlayer { get ; private set ; }
241
242
242
243
/// <summary>
243
244
/// Gets if the object is owned by the local player or if the object is the local player object
244
245
/// </summary>
245
- public bool IsOwner => NetworkObject . IsOwner ;
246
+ public bool IsOwner { get ; internal set ; }
246
247
247
248
/// <summary>
248
249
/// Gets if we are executing as server
249
250
/// </summary>
250
- protected bool IsServer => IsRunning && NetworkManager . IsServer ;
251
+ protected bool IsServer { get ; private set ; }
251
252
252
253
/// <summary>
253
254
/// Gets if we are executing as client
254
255
/// </summary>
255
- protected bool IsClient => IsRunning && NetworkManager . IsClient ;
256
+ protected bool IsClient { get ; private set ; }
257
+
256
258
257
259
/// <summary>
258
260
/// Gets if we are executing as Host, I.E Server and Client
259
261
/// </summary>
260
- protected bool IsHost => IsRunning && NetworkManager . IsHost ;
261
-
262
- private bool IsRunning => NetworkManager && NetworkManager . IsListening ;
262
+ protected bool IsHost { get ; private set ; }
263
263
264
264
/// <summary>
265
265
/// Gets Whether or not the object has a owner
266
266
/// </summary>
267
- public bool IsOwnedByServer => NetworkObject . IsOwnedByServer ;
267
+ public bool IsOwnedByServer { get ; internal set ; }
268
268
269
269
/// <summary>
270
270
/// Used to determine if it is safe to access NetworkObject and NetworkManager from within a NetworkBehaviour component
271
271
/// Primarily useful when checking NetworkObject/NetworkManager properties within FixedUpate
272
272
/// </summary>
273
- public bool IsSpawned => HasNetworkObject ? NetworkObject . IsSpawned : false ;
273
+ public bool IsSpawned { get ; internal set ; }
274
274
275
275
internal bool IsBehaviourEditable ( )
276
276
{
@@ -327,12 +327,12 @@ public NetworkObject NetworkObject
327
327
/// <summary>
328
328
/// Gets the NetworkId of the NetworkObject that owns this NetworkBehaviour
329
329
/// </summary>
330
- public ulong NetworkObjectId => NetworkObject . NetworkObjectId ;
330
+ public ulong NetworkObjectId { get ; internal set ; }
331
331
332
332
/// <summary>
333
333
/// Gets NetworkId for this NetworkBehaviour from the owner NetworkObject
334
334
/// </summary>
335
- public ushort NetworkBehaviourId => NetworkObject . GetNetworkBehaviourOrderIndex ( this ) ;
335
+ public ushort NetworkBehaviourId { get ; internal set ; }
336
336
337
337
/// <summary>
338
338
/// Internally caches the Id of this behaviour in a NetworkObject. Makes look-up faster
@@ -352,7 +352,47 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId)
352
352
/// <summary>
353
353
/// Gets the ClientId that owns the NetworkObject
354
354
/// </summary>
355
- public ulong OwnerClientId => NetworkObject . OwnerClientId ;
355
+ public ulong OwnerClientId { get ; internal set ; }
356
+
357
+ /// <summary>
358
+ /// Updates properties with network session related
359
+ /// dependencies such as a NetworkObject's spawned
360
+ /// state or NetworkManager's session state.
361
+ /// </summary>
362
+ internal void UpdateNetworkProperties ( )
363
+ {
364
+ // Set NetworkObject dependent properties
365
+ if ( NetworkObject != null )
366
+ {
367
+ // Set identification related properties
368
+ NetworkObjectId = NetworkObject . NetworkObjectId ;
369
+ IsLocalPlayer = NetworkObject . IsLocalPlayer ;
370
+
371
+ // This is "OK" because GetNetworkBehaviourOrderIndex uses the order of
372
+ // NetworkObject.ChildNetworkBehaviours which is set once when first
373
+ // accessed.
374
+ NetworkBehaviourId = NetworkObject . GetNetworkBehaviourOrderIndex ( this ) ;
375
+
376
+ // Set ownership related properties
377
+ IsOwnedByServer = NetworkObject . IsOwnedByServer ;
378
+ IsOwner = NetworkObject . IsOwner ;
379
+ OwnerClientId = NetworkObject . OwnerClientId ;
380
+
381
+ // Set NetworkManager dependent properties
382
+ if ( NetworkManager != null )
383
+ {
384
+ IsHost = NetworkManager . IsListening && NetworkManager . IsHost ;
385
+ IsClient = NetworkManager . IsListening && NetworkManager . IsClient ;
386
+ IsServer = NetworkManager . IsListening && NetworkManager . IsServer ;
387
+ }
388
+ }
389
+ else // Shouldn't happen, but if so then set the properties to their default value;
390
+ {
391
+ OwnerClientId = NetworkObjectId = default ;
392
+ IsOwnedByServer = IsOwner = IsHost = IsClient = IsServer = default ;
393
+ NetworkBehaviourId = default ;
394
+ }
395
+ }
356
396
357
397
/// <summary>
358
398
/// Gets called when the <see cref="NetworkObject"/> gets spawned, message handlers are ready to be registered and the network is setup.
@@ -366,21 +406,41 @@ public virtual void OnNetworkDespawn() { }
366
406
367
407
internal void InternalOnNetworkSpawn ( )
368
408
{
409
+ IsSpawned = true ;
369
410
InitializeVariables ( ) ;
411
+ UpdateNetworkProperties ( ) ;
412
+ OnNetworkSpawn ( ) ;
370
413
}
371
414
372
- internal void InternalOnNetworkDespawn ( ) { }
415
+ internal void InternalOnNetworkDespawn ( )
416
+ {
417
+ IsSpawned = false ;
418
+ UpdateNetworkProperties ( ) ;
419
+ OnNetworkDespawn ( ) ;
420
+ }
373
421
374
422
/// <summary>
375
423
/// Gets called when the local client gains ownership of this object
376
424
/// </summary>
377
425
public virtual void OnGainedOwnership ( ) { }
378
426
427
+ internal void InternalOnGainedOwnership ( )
428
+ {
429
+ UpdateNetworkProperties ( ) ;
430
+ OnGainedOwnership ( ) ;
431
+ }
432
+
379
433
/// <summary>
380
434
/// Gets called when we loose ownership of this object
381
435
/// </summary>
382
436
public virtual void OnLostOwnership ( ) { }
383
437
438
+ internal void InternalOnLostOwnership ( )
439
+ {
440
+ UpdateNetworkProperties ( ) ;
441
+ OnLostOwnership ( ) ;
442
+ }
443
+
384
444
/// <summary>
385
445
/// Gets called when the parent NetworkObject of this NetworkBehaviour's NetworkObject has changed
386
446
/// </summary>
0 commit comments