- Joined
- Sep 26, 2009
- Messages
- 9,534
This exists primarily to improve things such as manual event execution as well as unify GUI actions into Conditions (see further below for how to enable "Wait" calls from your triggers).
How it works: by hooking the natives "TriggerAddAction" and "TriggerAddCondition + Condition", I can extract the function that is being passed to the natives and, subsequently, call that function manually instead of letting the trigger spit it out.
So with GUI triggers with conditions and actions, the conditions will be called manually and the actions will be called manually if the conditions are true.
If you don't want your Lua resource be affected by this, then I recommend using (Filter (or any other kind of boolexpr)) instead of (Condition) when defining conditions for your Lua trigger. I am not sure if anyone is using TriggerActions in Lua, nor why, but if you need multiple TriggerActions attached to the same trigger, then this resource will not work. A better solution for such odd scenarios is to switch your Actions to Conditions, or to use ExecuteFunc if you need waits.
Requires: [Lua] - Hook
To enable your trigger actions to use "Waits" again (ie. PolledWait), use [Lua] Perfect PolledWait (GUI-friendly) and put the following one line of Custom Script at the VERY TOP of your trigger actions list: if EnableWaits() then return end. The system will handle the rest.
How it works: by hooking the natives "TriggerAddAction" and "TriggerAddCondition + Condition", I can extract the function that is being passed to the natives and, subsequently, call that function manually instead of letting the trigger spit it out.
So with GUI triggers with conditions and actions, the conditions will be called manually and the actions will be called manually if the conditions are true.
If you don't want your Lua resource be affected by this, then I recommend using (Filter (or any other kind of boolexpr)) instead of (Condition) when defining conditions for your Lua trigger. I am not sure if anyone is using TriggerActions in Lua, nor why, but if you need multiple TriggerActions attached to the same trigger, then this resource will not work. A better solution for such odd scenarios is to switch your Actions to Conditions, or to use ExecuteFunc if you need waits.
Requires: [Lua] - Hook
Lua:
if Hook then --https://www.hiveworkshop.com/threads/hook.339153
--Lua Fast Triggers v1.4.1.0
--Completely overwrites the BJ "ConditionalTriggerExecute" rather than hooking it, due to performance reasons.
local _PRIORITY = -1 --Specify the hook priority for TriggerAddAction and TriggerAddCondition
local cMap = {}
local aMap = {}
local lastCondFunc
local waitFunc
--hook.args = {1:code}
Hook.add("Condition",
function(hook)
lastCondFunc = hook.args[1]
end)
--hook.args = {1:trigger, 2:boolexpr}
Hook.add("TriggerAddCondition",
function(hook)
if lastCondFunc then
local trig = hook.args[1]
local cond = lastCondFunc
cMap[trig] = cond --map the condition function to the trigger.
aMap[trig] = aMap[trig] or DoNothing
lastCondFunc = nil
hook.args[2] = Filter(
function()
if cond() then --Call the triggerconditions manually.
waitFunc = aMap[trig]
waitFunc() --If this was caused by an event, call the trigger actions manually.
end --always return nil to prevent WC3 from executing any trigger actions.
end)
end
end, _PRIORITY)
--hook.args = {1:trigger, 2:code}
Hook.add("TriggerAddAction",
function(hook)
local act = hook.args[2]
aMap[hook.args[1]] = act
hook.args[2] =
function()
waitFunc = act
waitFunc() --If this was caused by an event, call the trigger actions manually.
end
end, _PRIORITY)
--hook.args = {1:trigger}
Hook.add("TriggerExecute",
function(hook)
waitFunc = aMap[hook.args[1]]
hook.skip = true
waitFunc()
end)
local skipNext
function EnableWaits()
if skipNext then
skipNext = nil
else
skipNext = true
coroutine.resume(coroutine.create(function()
waitFunc()
end))
return true
end
end
function ConditionalTriggerExecute(trig)
local c = cMap[trig]
if c and not c() then return end
local a = aMap[trig]
if a then a() end
end
function GetTriggerActionFunc(trig)
return aMap[trig]
end
function GetTriggerConditionFunc(trig)
return cMap[trig]
end
end
To enable your trigger actions to use "Waits" again (ie. PolledWait), use [Lua] Perfect PolledWait (GUI-friendly) and put the following one line of Custom Script at the VERY TOP of your trigger actions list: if EnableWaits() then return end. The system will handle the rest.
-
Want to wait
-
Events
-
-------- Some event
-
-
Conditions
-
-------- Some conditions
-
-
Actions
-
Custom script: if EnableWaits() then return end
-
Wait - 0.50 seconds of game time
-
-------- Do further actions here.
-
-
Lua:
-- This is between 16-18 times slower than what is done with the Fast Triggers approach.
function ConditionalTriggerExecute(trig)
if IsTriggerEnabled(trig) and TriggerEvaluate(trig) then
TriggerExecute(trig)
end
end
Last edited: