diff --git a/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs b/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs index aeee22ce1b..02d99e4eac 100644 --- a/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs +++ b/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs @@ -22,18 +22,25 @@ namespace OpenRA.Mods.RA.Scripting OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnAddedToWorld, OnRemovedFromWorld }; [Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")] - public class ScriptTriggersInfo : TraitInfo { } + public class ScriptTriggersInfo : ITraitInfo + { + public object Create(ActorInitializer init) { return new ScriptTriggers(init.world); } + } public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyObjectivesUpdated, INotifyCapture, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable { + readonly World world; + public event Action OnKilledInternal = _ => { }; public event Action OnRemovedInternal = _ => { }; public Dictionary>> Triggers = new Dictionary>>(); - public ScriptTriggers() + public ScriptTriggers(World world) { - foreach (var t in Enum.GetValues(typeof(Trigger)).Cast()) + this.world = world; + + foreach (Trigger t in Enum.GetValues(typeof(Trigger))) Triggers.Add(t, new List>()); } @@ -265,20 +272,22 @@ namespace OpenRA.Mods.RA.Scripting public void Clear(Trigger trigger) { - Triggers[trigger].Clear(); + world.AddFrameEndTask(w => + { + Triggers[trigger].Select(p => p.First).Do(f => f.Dispose()); + Triggers[trigger].Clear(); + }); } public void ClearAll() { - foreach (var trigger in Triggers) - trigger.Value.Clear(); + foreach (Trigger t in Enum.GetValues(typeof(Trigger))) + Clear(t); } public void Dispose() { - var pairs = Triggers.Values; - pairs.SelectMany(l => l).Select(p => p.First).Do(f => f.Dispose()); - pairs.Do(l => l.Clear()); + ClearAll(); } } }