- Joined
- Jun 30, 2017
- Messages
- 50
A function that offers you the ability to run a function (with or without parameters) after a specified delay, without using polled waits or manually setting up timers.
Requires [Lua] TimerUtils
Use examples:
Edit (29.12.2019): Swapped the argument order in DelayedAction function as suggested by @Tasyen.
Edit (3.1.2020): Replaced DestroyTimer with ReleaseTimer. DelayedAction now returns the timer, in case said action needs to be paused or something.
Requires [Lua] TimerUtils
Lua:
-- Delayed Action v1.0.1 by Insanity_AI
-- requires https://www.hiveworkshop.com/threads/lua-timerutils.316957/
--[[Description:
A function that offers you to run a function with arguments after some delay,
without the use of polled waits, or manually setting up timers.
]]
--[[Use example:
DelayedAction(3.00, RemoveUnit, GetTriggerUnit())
will remove the triggering unit of some trigger after 3 seconds.
]]
function DelayedAction(delay, func, ...)
if type(func) == "nil" or type(delay) == "nil" or delay < 0.00 then
return
end
local timer = NewTimer({func,...})
TimerStart(timer, delay, false, DelayedActionExecute)
return timer
end
function DelayedActionExecute()
local data = GetTimerData()
local func = table.unpack(data,1)
table.remove(data,1)
ReleaseTimer(GetExpiredTimer())
pcall(func,table.unpack(data))
end
Use examples:
Code:
DelayedAction(1.00, RemoveUnit, GetTriggerUnit()) --will remove a unit after 1 second
DelayedAction(2.00, SomeFunction) -- will run "SomeFunction"(which doesn't require parameters) after 2 seconds
Edit (29.12.2019): Swapped the argument order in DelayedAction function as suggested by @Tasyen.
Edit (3.1.2020): Replaced DestroyTimer with ReleaseTimer. DelayedAction now returns the timer, in case said action needs to be paused or something.
Attachments
Last edited: