Timer Script

Post Reply
Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Timer Script

Post by Nyyrazzilyss »

I'd posted this in another thread. With numerous timer related questions, better to just give it it's own thread.

The below script includes a couple easy to use timer related commands (and some documentation/commenting!)
Code: [show] | [select all] lua
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

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Timer Script

Post by Nyyrazzilyss »

Here's a quick example using the above:
Code: [show] | [select all] lua
timer:init()
timer:set("bomb", 10)

function bomb(reason)
	if reason == "timer" then
		echo("I am a " .. timer:get("bomb") .. " second bomb.\n")
	else
		echo("BOOM!\n")
	end
end
-- output --
I am a 9 second bomb.
I am a 8 second bomb.
I am a 7 second bomb.
I am a 6 second bomb.
I am a 5 second bomb.
I am a 4 second bomb.
I am a 3 second bomb.
I am a 2 second bomb.
I am a 1 second bomb.
BOOM!

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Timer Script

Post by Vadi »

Looks good! Did you mean to have it in Help? Maybe it should be in scripts?

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Timer Script

Post by Nyyrazzilyss »

Thanks - You can delete this post. I hadn't exactly looked at the code since I wrote it, and looking back at it something stood out right away that should be changed to simplify it. I'll rewrite and repost in the scripts forum.

Post Reply