Skip to content

Commit

Permalink
Reduce transform resolves in RecursiveDeleteEntity() (#4468)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Sep 30, 2023
1 parent 889b835 commit 3b6adeb
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Robust.Shared/GameObjects/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,13 @@ public virtual void DeleteEntity(EntityUid? uid)
// Notify all entities they are being terminated prior to detaching & deleting
RecursiveFlagEntityTermination(e, meta);

var xform = TransformQuery.GetComponent(e);
TransformComponent? parentXform = null;
if (xform.ParentUid.IsValid())
TransformQuery.Resolve(xform.ParentUid, ref parentXform);

// Then actually delete them
RecursiveDeleteEntity(e, meta);
RecursiveDeleteEntity(e, meta, xform, parentXform);
}

private void RecursiveFlagEntityTermination(
Expand Down Expand Up @@ -471,19 +476,22 @@ private void RecursiveFlagEntityTermination(

private void RecursiveDeleteEntity(
EntityUid uid,
MetaDataComponent metadata)
MetaDataComponent metadata,
TransformComponent transform,
TransformComponent? parentXform)
{
DebugTools.Assert(transform.ParentUid.IsValid() == (parentXform != null));
DebugTools.Assert(parentXform == null || parentXform.ChildEntities.Contains(uid));

// Note about this method: #if EXCEPTION_TOLERANCE is not used here because we're gonna it in the future...
var netEntity = GetNetEntity(uid, metadata);
var transform = TransformQuery.GetComponent(uid);

// Detach the base entity to null before iterating over children
// This also ensures that the entity-lookup updates don't have to be re-run for every child (which recurses up the transform hierarchy).
if (transform.ParentUid != EntityUid.Invalid)
{
try
{
_xforms.DetachParentToNull(uid, transform);
_xforms.DetachParentToNull(uid, transform, parentXform);
}
catch (Exception e)
{
Expand All @@ -495,7 +503,10 @@ private void RecursiveDeleteEntity(
{
try
{
RecursiveDeleteEntity(child, MetaQuery.GetComponent(child));
var childMeta = MetaQuery.GetComponent(child);
var childXform = TransformQuery.GetComponent(child);
DebugTools.AssertEqual(childXform.ParentUid, uid);
RecursiveDeleteEntity(child, childMeta, childXform, transform);
}
catch(Exception e)
{
Expand Down Expand Up @@ -538,7 +549,7 @@ private void RecursiveDeleteEntity(
_eventBus.OnEntityDeleted(uid);
Entities.Remove(uid);
// Need to get the ID above before MetadataComponent shutdown but only remove it after everything else is done.
NetEntityLookup.Remove(netEntity);
NetEntityLookup.Remove(metadata.NetEntity);
}

public virtual void QueueDeleteEntity(EntityUid? uid)
Expand Down

0 comments on commit 3b6adeb

Please sign in to comment.