Here's a little something I wrote up as an example of using/creating timers, as well as creating a library. As such, it can also stand as an example of using LibStub for your own libraries.
The code is written so that the API works in an object-oriented fashion and so this can also serve as a bit of an example on doing OOP style code (though it doesn't do full OOP style class declarations).
[SIZE=+2]LibTimers[/SIZE]
A general library for the creation and use of timers in Runes of Magic.
LibTimers is designed so that it can be used either as a stand-alone library add-on or be embedded into other add-ons without interference, or code duplication. That is, only one copy of the library resides in memory even if both methods are present.
The ZIP file provided here acts as a stand-alone version, but the Lua file can be included into other projects.
LibTimers requires the use of LibStub (provided).
See the file
Manual.txt for full usage and API description.
[SIZE=+2]Example Usage[/SIZE]
The following will get the library (via LibStub) then create and start a repeating 5 second timer.
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-- Get a reference to the library
local Lib = LibStub("LibTimers")
if not Lib then
DEFAULT_CHAT_FRAME:AddMessage("LibTimers not found, aborting")
return
end
local function TimerCallback()
DEFAULT_CHAT_FRAME:AddMessage("Tick!")
end
-- Create a timer object
local Timer = Lib:CreateTimer()
-- Set the timer to repeat every 5 seconds
Timer:SetDuration(5)
Timer:SetMode(Lib.Modes.REPEATING)
-- Set a function for the timer to call
Timer:SetCallback(TimerCallback)
-- Start the timer
Timer:Start()
|