Don't spawn new actors before all RemovedFromWorld callbacks have run

This commit is contained in:
abcdefg30
2017-08-08 16:05:53 +02:00
committed by reaperrr
parent 22d7031819
commit d1ab421240

View File

@@ -53,11 +53,13 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); } public override object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); }
} }
public class SpawnActorOnDeath : ConditionalTrait<SpawnActorOnDeathInfo>, INotifyKilled public class SpawnActorOnDeath : ConditionalTrait<SpawnActorOnDeathInfo>, INotifyKilled, INotifyRemovedFromWorld
{ {
readonly string faction; readonly string faction;
readonly bool enabled; readonly bool enabled;
Player attackingPlayer;
public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info) public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
: base(info) : base(info)
{ {
@@ -65,7 +67,7 @@ namespace OpenRA.Mods.Common.Traits
faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName; faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
} }
public void Killed(Actor self, AttackInfo e) void INotifyKilled.Killed(Actor self, AttackInfo e)
{ {
if (!enabled || IsTraitDisabled) if (!enabled || IsTraitDisabled)
return; return;
@@ -79,10 +81,13 @@ namespace OpenRA.Mods.Common.Traits
if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType)) if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType))
return; 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)
{ {
// Actor has been disposed by something else before its death (for example `Enter`). if (attackingPlayer == null)
if (self.Disposed)
return; return;
var td = new TypeDictionary var td = new TypeDictionary
@@ -96,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.OwnerType == OwnerType.Victim) if (Info.OwnerType == OwnerType.Victim)
td.Add(new OwnerInit(self.Owner)); td.Add(new OwnerInit(self.Owner));
else if (Info.OwnerType == OwnerType.Killer) else if (Info.OwnerType == OwnerType.Killer)
td.Add(new OwnerInit(e.Attacker.Owner)); td.Add(new OwnerInit(attackingPlayer));
else else
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner))); td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));
@@ -110,8 +115,7 @@ namespace OpenRA.Mods.Common.Traits
.Select(ihm => ihm.HuskActor(self)) .Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null); .FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? Info.Actor, td); self.World.AddFrameEndTask(w => w.CreateActor(huskActor ?? Info.Actor, td));
});
} }
} }
} }