From d1ab421240135145389cec7bf118e6ac94738a79 Mon Sep 17 00:00:00 2001 From: abcdefg30 Date: Tue, 8 Aug 2017 16:05:53 +0200 Subject: [PATCH] Don't spawn new actors before all RemovedFromWorld callbacks have run --- .../Traits/SpawnActorOnDeath.cs | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs b/OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs index 90017e05a1c4..068e81d201ef 100644 --- a/OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs +++ b/OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs @@ -53,11 +53,13 @@ public class SpawnActorOnDeathInfo : ConditionalTraitInfo public override object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); } } - public class SpawnActorOnDeath : ConditionalTrait, INotifyKilled + public class SpawnActorOnDeath : ConditionalTrait, INotifyKilled, INotifyRemovedFromWorld { readonly string faction; readonly bool enabled; + Player attackingPlayer; + public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info) : base(info) { @@ -65,7 +67,7 @@ public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info) faction = init.Contains() ? init.Get() : init.Self.Owner.Faction.InternalName; } - public void Killed(Actor self, AttackInfo e) + void INotifyKilled.Killed(Actor self, AttackInfo e) { if (!enabled || IsTraitDisabled) return; @@ -79,39 +81,41 @@ public void Killed(Actor self, AttackInfo e) if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType)) return; - self.World.AddFrameEndTask(w => + attackingPlayer = e.Attacker.Owner; + } + + // Don't add the new actor to the world before all RemovedFromWorld callbacks have run + void INotifyRemovedFromWorld.RemovedFromWorld(Actor self) + { + if (attackingPlayer == null) + return; + + var td = new TypeDictionary { - // Actor has been disposed by something else before its death (for example `Enter`). - if (self.Disposed) - return; - - var td = new TypeDictionary - { - new ParentActorInit(self), - new LocationInit(self.Location + Info.Offset), - new CenterPositionInit(self.CenterPosition), - new FactionInit(faction) - }; - - if (Info.OwnerType == OwnerType.Victim) - td.Add(new OwnerInit(self.Owner)); - else if (Info.OwnerType == OwnerType.Killer) - td.Add(new OwnerInit(e.Attacker.Owner)); - else - td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner))); - - if (Info.SkipMakeAnimations) - td.Add(new SkipMakeAnimsInit()); - - foreach (var modifier in self.TraitsImplementing()) - modifier.ModifyDeathActorInit(self, td); - - var huskActor = self.TraitsImplementing() - .Select(ihm => ihm.HuskActor(self)) - .FirstOrDefault(a => a != null); - - w.CreateActor(huskActor ?? Info.Actor, td); - }); + new ParentActorInit(self), + new LocationInit(self.Location + Info.Offset), + new CenterPositionInit(self.CenterPosition), + new FactionInit(faction) + }; + + if (Info.OwnerType == OwnerType.Victim) + td.Add(new OwnerInit(self.Owner)); + else if (Info.OwnerType == OwnerType.Killer) + td.Add(new OwnerInit(attackingPlayer)); + else + td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner))); + + if (Info.SkipMakeAnimations) + td.Add(new SkipMakeAnimsInit()); + + foreach (var modifier in self.TraitsImplementing()) + modifier.ModifyDeathActorInit(self, td); + + var huskActor = self.TraitsImplementing() + .Select(ihm => ihm.HuskActor(self)) + .FirstOrDefault(a => a != null); + + self.World.AddFrameEndTask(w => w.CreateActor(huskActor ?? Info.Actor, td)); } } }