Page 1 of 1
Updating timers
Posted: Sun May 21, 2017 2:02 pm
by lucifer
Hello!
I am trying to start a "countdown timer" when I see a particular line of text. This works well, but then I need to update the timer when I receive another line of text. Example:
1) I see the substring "You turn into a vampire." and start a countdown from 30 to 0.
2) I see the substring "The moon is full." and now I want to delete the previous countdown timer and start a new one counting down from 25. I am doing this in the same spot where the previous one was being drawn, so it must be removed.
I am currently doing 1), but not 2) as all my approaches fail, since I don't know how to keep track of the variable "id" between calls to the trigger. The input is specified on line 0 and line 1, so I will be checking something like: if line == "The moon is full" then <remove the old timer and start a new one> end.
totalTicks = 35
-- Countdown for regular shape shift
for tick=1, totalTicks do
id = tempTimer(tick,
function()
GUI.EffectIcon11:echo(totalTicks - tick)
-- Last tick
if tick == totalTicks then
GUI.EffectIcon11:echo("")
end
end)
end
Re: Updating timers
Posted: Wed May 24, 2017 2:36 pm
by Vadi
Using this way, I think you'd need to keep track of all the id's you create in a table and then delete all the timers when you have to delete the previous timer prematurely.
You could also try using a permTimer that counts the amount of ticks instead and self-deletes when you've got enough tickets or something needs to change - might be simpler!
Re: Updating timers
Posted: Thu May 25, 2017 1:10 am
by Nyyrazzilyss
Below is the timer script I wrote/use. It creates a single incrementing master timer, and then provides functions to create new countdown timers based off that master timer. They're named (not numbered), and if a function exists matching their name that function is called every second + on expiry.
timer = timer or {}
timer.values = timer.values or {}
-- Usage:
--
-- timer:init() - create master timer
-- timer:get(xname) - return current value or nil
-- timer:set(xname, xduration) -- set timer 'xname' to value 'xduration' (duration=nil removes timer)
-- timer:custom - if created, this script will be called every second after timers update
--
-- If a function exists matching the name of the timer, it will be called every second with ("timer", functionname)
-- passed as an argument. When timers expire (reach 0) the timer will be deleted and the associated
-- function will be called with arguments ("expire", functionname)
function timer:init()
if exists("MudTimer", "timer") == 0 then
permTimer("MudTimer", "", 1, [[timer:script()]] )
end
enableTimer("MudTimer")
end
function timer:set(xname, xduration)
self.values[xname] = xduration
end
function timer:get(xname)
if self.values[xname] == nil then
return(nil)
end
return (self.values[xname])
end
function timer:script()
-- decrement all timers
for k, v in pairs(timer.values) do
timer.values[k] = timer.values[k]-1
-- Timer has reached 0 (expired)
if timer.values[k] < 1 and k ~= "" then
timer.values[k] = nil
if _G[k] == nil then
--echoDebug("<red>[ " .. k .. " function is nil]\n")
else
_G[k]("expire", k)
end
else
-- have timer called every second with "timer" as argument
if _G[k] == nil then
--echoDebug("<red>[ " .. k .. " function is nil]\n")
else
_G[k]("timer", k)
end
end
end
if timer.custom ~= nil then
timer:custom()
end
end
Re: Updating timers
Posted: Thu May 25, 2017 7:05 pm
by lucifer
Thanks a lot for the advice! permTimer seems to solve the problem for me. I came up with the following solution, which is simple enough.
I don't really like that I need to look for a specific text, e.g. "You call upon your ..." and I also ought to search for that particular substring in the trigger definition, but I guess there is no way to access the line called "0" and the one called "1" etc. Anyway, this works:
-- Lycanthrope-specific timers
if line == "You call upon your lycanthrope powers!!" then
formDuration = 315
enableTimer("formtimer")
elseif line == "You are reminded of your ties to the moon." then
formDuration = 25
enableTimer("formtimer")
end
function tick()
GUI.EffectIcon11:echo(formDuration)
formDuration = formDuration - 1
if formDuration == 0 then
disableTimer("formtimer")
end
end
if exists("formtimer", "timer") == 0 then
permTimer("formtimer", "", 1, [[ tick() ]])
end