public void Dispose() { // If CurrentActivity isn't null, run OnActorDisposeOuter in case some cleanups are needed. // This should be done before the FrameEndTask to avoid dependency issues. CurrentActivity?.OnActorDisposeOuter(this); // Allow traits/activities to prevent a race condition when they depend on disposing the actor (e.g. Transforms) WillDispose = true; World.AddFrameEndTask(w => { if (Disposed) { return; } if (IsInWorld) { World.Remove(this); } foreach (var t in TraitsImplementing <INotifyActorDisposing>()) { t.Disposing(this); } World.TraitDict.RemoveActor(this); Disposed = true; luaInterface?.Value.OnActorDestroyed(); }); }