From 3e93242929583a538261a1d0ff2a1a13e8d5791a Mon Sep 17 00:00:00 2001 From: abcdefg30 Date: Fri, 5 Oct 2018 01:10:58 +0200 Subject: [PATCH] Fix OnAllRemovedFromWorld only triggering once --- .../Scripting/Global/TriggerGlobal.cs | 26 +++++++++++++++++-- .../Scripting/ScriptTriggers.cs | 4 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs index 32762170bd..02aeecada7 100644 --- a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs @@ -211,8 +211,27 @@ namespace OpenRA.Mods.Common.Scripting return; if (!group.Any()) - using (f) - f.Call().Dispose(); + { + // Functions can only be .Call()ed once, so operate on a copy so we can reuse it later + var temp = (LuaFunction)f.CopyReference(); + using (temp) + temp.Call().Dispose(); + } + } + catch (Exception e) + { + Context.FatalError(e.Message); + } + }; + + Action onMemberAdded = m => + { + try + { + if (!actors.Contains(m) || group.Contains(m)) + return; + + group.Add(m); } catch (Exception e) { @@ -221,7 +240,10 @@ namespace OpenRA.Mods.Common.Scripting }; foreach (var a in group) + { GetScriptTriggers(a).OnRemovedInternal += onMemberRemoved; + GetScriptTriggers(a).OnAddedInternal += onMemberAdded; + } } [Desc("Call a function when this actor is captured. The callback function " + diff --git a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs index 7c69e79285..1394290e43 100644 --- a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs +++ b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs @@ -43,6 +43,7 @@ namespace OpenRA.Mods.Common.Scripting public event Action OnKilledInternal = _ => { }; public event Action OnCapturedInternal = _ => { }; public event Action OnRemovedInternal = _ => { }; + public event Action OnAddedInternal = _ => { }; public event Action OnProducedInternal = (a, b) => { }; public event Action OnOtherProducedInternal = (a, b) => { }; @@ -340,6 +341,9 @@ namespace OpenRA.Mods.Common.Scripting return; } } + + // Run any internally bound callbacks + OnAddedInternal(self); } void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)