Skip to content

Commit f640138

Browse files
committed
ensure internal state is completely reset on behaviour despawn
1 parent b33fea8 commit f640138

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -502,23 +502,11 @@ public NetworkManager NetworkManager
502502
/// </summary>
503503
public bool HasAuthority { get; internal set; }
504504

505-
internal NetworkClient LocalClient { get; private set; }
506505

507506
/// <summary>
508507
/// Gets whether the client is the distributed authority mode session owner.
509508
/// </summary>
510-
public bool IsSessionOwner
511-
{
512-
get
513-
{
514-
if (LocalClient == null)
515-
{
516-
return false;
517-
}
518-
519-
return LocalClient.IsSessionOwner;
520-
}
521-
}
509+
public bool IsSessionOwner { get; private set; }
522510

523511
/// <summary>
524512
/// Gets whether the server (local or remote) is a host.
@@ -675,12 +663,41 @@ internal void UpdateNetworkProperties()
675663
IsHost = networkManager.IsListening && networkManager.IsHost;
676664
IsClient = networkManager.IsListening && networkManager.IsClient;
677665
IsServer = networkManager.IsListening && networkManager.IsServer;
678-
LocalClient = networkManager.LocalClient;
666+
IsSessionOwner = networkManager.IsListening && networkManager.LocalClient.IsSessionOwner;
679667
HasAuthority = networkObject.HasAuthority;
680668
ServerIsHost = networkManager.IsListening && networkManager.ServerIsHost;
681669
}
682670
}
683671

672+
private void ResetAllFields()
673+
{
674+
m_NetworkObject = null;
675+
m_NetworkManager = null;
676+
RpcTarget = null;
677+
678+
// Set identification related properties
679+
NetworkObjectId = default;
680+
IsLocalPlayer = false;
681+
682+
// This is "OK" because GetNetworkBehaviourOrderIndex uses the order of
683+
// NetworkObject.ChildNetworkBehaviours which is set once when first
684+
// accessed.
685+
NetworkBehaviourId = default;
686+
687+
// Set ownership related properties
688+
IsOwnedByServer = false;
689+
IsOwner = false;
690+
OwnerClientId = default;
691+
692+
// Set NetworkManager dependent properties
693+
IsHost = false;
694+
IsClient = false;
695+
IsServer = false;
696+
IsSessionOwner = false;
697+
HasAuthority = false;
698+
ServerIsHost = false;
699+
}
700+
684701
/// <summary>
685702
/// Only for use in distributed authority mode.
686703
/// Invoked only on the authority instance when a <see cref="NetworkObject"/> is deferring its despawn on non-authoritative instances.
@@ -762,6 +779,7 @@ internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject n
762779
{
763780
m_NetworkObject = networkObject;
764781
m_NetworkManager = networkManager;
782+
RpcTarget = networkManager.RpcTarget;
765783

766784
UpdateNetworkProperties();
767785

@@ -872,6 +890,8 @@ internal void InternalOnNetworkDespawn()
872890
{
873891
NetworkVariableFields[i].Deinitialize();
874892
}
893+
894+
ResetAllFields();
875895
}
876896

877897
/// <summary>
@@ -1553,7 +1573,15 @@ internal virtual void InternalOnDestroy()
15531573
/// </summary>
15541574
public virtual void OnDestroy()
15551575
{
1556-
InternalOnDestroy();
1576+
try
1577+
{
1578+
InternalOnDestroy();
1579+
}
1580+
catch (Exception ex)
1581+
{
1582+
Debug.LogException(ex);
1583+
}
1584+
15571585
if (m_NetworkObject != null && m_NetworkObject.IsSpawned && IsSpawned)
15581586
{
15591587
// If the associated NetworkObject is still spawned then this
@@ -1574,12 +1602,12 @@ public virtual void OnDestroy()
15741602
}
15751603

15761604

1577-
for (int i = 0; i < NetworkVariableFields.Count; i++)
1605+
foreach (var networkVar in NetworkVariableFields)
15781606
{
1579-
NetworkVariableFields[i].Dispose();
1607+
networkVar.Dispose();
15801608
}
15811609

1582-
m_NetworkObject = null;
1610+
ResetAllFields();
15831611
}
15841612
}
15851613
}

0 commit comments

Comments
 (0)