Skip to content

Commit aa8b2f4

Browse files
update
Modifications from 1:1 PR review session with Emma. Removed some redundant logic. Re-organized a few areas for better readability. Renamed the `AttachableBehaviour.AttachableNode` to `AttachableBehaviour.InternalAttachableNode`. Removed the authority check within `AttachableNode.OnNetworkPreDespawn`.
1 parent bb04aa9 commit aa8b2f4

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableBehaviour.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public enum AttachState
198198
/// If attached, attaching, or detaching this will be the <see cref="AttachableNode"/> this <see cref="AttachableBehaviour"/> instance is attached to.
199199
/// </summary>
200200
protected AttachableNode m_AttachableNode { get; private set; }
201-
internal AttachableNode AttachableNode => m_AttachableNode;
201+
internal AttachableNode InternalAttachableNode => m_AttachableNode;
202202

203203
private NetworkBehaviourReference m_AttachedNodeReference = new NetworkBehaviourReference(null);
204204
private Vector3 m_OriginalLocalPosition;
@@ -287,36 +287,37 @@ public override void OnNetworkPreDespawn()
287287
private void UpdateAttachedState()
288288
{
289289
var attachableNode = (AttachableNode)null;
290-
var isAttaching = m_AttachedNodeReference.TryGet(out attachableNode, NetworkManager);
291-
var preState = isAttaching ? AttachState.Attaching : AttachState.Detaching;
290+
var referenceHasNode = m_AttachedNodeReference.TryGet(out attachableNode, NetworkManager);
292291

292+
/////////////////////////////////////////////////////////////
293293
// Exit early if we are already in the correct attached state and the incoming
294294
// AttachableNode reference is the same as the local AttachableNode property.
295295
if (attachableNode == m_AttachableNode &&
296-
((isAttaching && m_AttachState == AttachState.Attached) ||
297-
(!isAttaching && m_AttachState == AttachState.Detached)))
296+
((referenceHasNode && m_AttachState == AttachState.Attached) ||
297+
(!referenceHasNode && m_AttachState == AttachState.Detached)))
298298
{
299299
return;
300300
}
301301

302-
var preNode = isAttaching ? attachableNode : m_AttachableNode;
303-
isAttaching = isAttaching && attachableNode != null;
302+
// If we are in an attaching state but the node is null then we are still not attaching.
303+
var isAttaching = referenceHasNode && attachableNode != null;
304304

305+
// If we are attached to some other AttachableNode, then detach from that before attaching to a new one.
305306
if (isAttaching && m_AttachableNode != null && m_AttachState == AttachState.Attached)
306307
{
307-
// If we are attached to some other AttachableNode, then detach from that before attaching to a new one.
308-
if (m_AttachableNode != attachableNode)
309-
{
310-
// Run through the same process without being triggerd by a NetVar update.
311-
UpdateAttachState(AttachState.Detaching, m_AttachableNode);
312-
InternalDetach();
313-
UpdateAttachState(AttachState.Detached, m_AttachableNode);
308+
// Run through the same process without being triggerd by a NetVar update.
309+
UpdateAttachState(AttachState.Detaching, m_AttachableNode);
310+
InternalDetach();
311+
UpdateAttachState(AttachState.Detached, m_AttachableNode);
314312

315-
m_AttachableNode.Detach(this);
316-
m_AttachableNode = null;
317-
}
313+
m_AttachableNode.Detach(this);
314+
m_AttachableNode = null;
318315
}
319316

317+
// Used for attaching or detatching notifications
318+
var preNode = referenceHasNode ? attachableNode : m_AttachableNode;
319+
var preState = referenceHasNode ? AttachState.Attaching : AttachState.Detaching;
320+
320321
// Change the state to attaching or detaching
321322
UpdateAttachState(preState, preNode);
322323

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected override void OnOwnershipChanged(ulong previous, ulong current)
5151
var attachables = NetworkObject.transform.GetComponentsInChildren<AttachableBehaviour>();
5252
foreach (var attachable in attachables)
5353
{
54-
if (attachable.AttachableNode == this)
54+
if (attachable.InternalAttachableNode == this)
5555
{
5656
m_AttachedBehaviours.Add(attachable);
5757
}
@@ -67,7 +67,7 @@ protected override void OnOwnershipChanged(ulong previous, ulong current)
6767
/// </remarks>
6868
public override void OnNetworkPreDespawn()
6969
{
70-
if (IsSpawned && HasAuthority && DetachOnDespawn)
70+
if (IsSpawned && DetachOnDespawn)
7171
{
7272
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
7373
{

com.unity.netcode.gameobjects/Tests/Runtime/AttachableBehaviourTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ private bool AllInstancesDetached()
387387
m_ErrorLog.AppendLine($"[Client-{networkManager.LocalClientId}][{attachable.name}] Did not have its event invoked!");
388388
}
389389

390-
if (attachable.AttachableNode != null)
390+
if (attachable.InternalAttachableNode != null)
391391
{
392-
var nodeHasAttachments = attachable.AttachableNode.HasAttachments ? $" {attachable.AttachableNode.name} still has attachments!" : "";
393-
m_ErrorLog.AppendLine($"[Client-{networkManager.LocalClientId}][{attachable.name}] Still refers to {attachable.AttachableNode.name}!{nodeHasAttachments}");
392+
var nodeHasAttachments = attachable.InternalAttachableNode.HasAttachments ? $" {attachable.InternalAttachableNode.name} still has attachments!" : "";
393+
m_ErrorLog.AppendLine($"[Client-{networkManager.LocalClientId}][{attachable.name}] Still refers to {attachable.InternalAttachableNode.name}!{nodeHasAttachments}");
394394
}
395395
}
396396
return m_ErrorLog.Length == 0;

0 commit comments

Comments
 (0)