Timed Functions

Current Version: 0.11

[Info]

Timed Functions is an Library / Development tool to aid in adding time-able functions to Runes of Magic. Rather then creating your own "timer" frames and setting up all your necessary functions and timer specific variables for a couple of simple functions you may want to fire at specific times, you can use this tool to simplify the amount of work you have to do. It also serves as a great addition to complex addon's that need a few timer functions.

[How To: Install]
The library is loaded to the current environment (_G) as a table with name "TimedFunctions".
There are two ways to load this module, the first way requires some work by you to make sure the module is loaded before usage.

The First Way: As an Independent AddOn (version 0.10 and lower)
Just copy the folder 'TimedFunctions' to RoM's 'interface\addons' folder.
Because addon's load at different times, there is a chance this library will not load before your addon does, which will break your addon so it is a good idea to check for the timer module before using it. The best way to use the timer module, and stop any possible breakage due to the module not being loaded before your addon, is to have the addon check for the timer module in the 'OnEvent' function of your addon, if it uses any events that is. An example would be something like:

local timer_loaded = false;

function UberAddon_OnEvent(this, event, arg1, arg2, arg3, arg4, arg5, arg6)
     if (not timer_loaded) then
          if (TimedFunctions) then
               timer1 = TimedFunctions:new("UberAddon");
               timer1:RegisterTimedFunction(UberAddon_Timer, 0.500);
               timer_loaded = true;
          end
     end

     -- do other event related stuff

end

Or something similar; so long as you DO NOT try to use the library before it is loaded.

The Second Way: As a Module (Recommended - version 0.11 and higher)

This way is only valid for versions 0.11 and higher, use the first way for older versions.

To use this module the second way you must include the timer module with your addon, and point to the module through your .toc file before you load any of your addon files. You can place the 'TimedFunctions.lua' file either in your addons path directly, or in a separate folder in the same path as your addon, for instance an 'inc' folder. You do not need, and it is recommended that you do not include at all, the two files 'TimedFunctions.toc' & 'TimedFunctions.xml' with the timer module when loading it the Second way.

An example without placing the module in a separate folder:

## Author: Dood
## Tester: N/A
## Title: Killer Dood Addon
## Version : 0.0.1

TimedFunctions.lua

UberDood.lua
UberDood.xml

An example with placing the module in a separate 'inc' folder:

## Author: Dood
## Tester: N/A
## Title: Killer Dood Addon
## Version : 0.0.1

inc/TimedFunctions.lua

UberDood.lua
UberDood.xml

[How To: API]
There are 5 API's you need to be aware of in order to use the library, however the bold are adequate, these are:

:new ([string]);

  • Creates a new timer object of name 'string'. String can be any name, use a prefix of your addon's name or the full name of your addon for instance. If you choose not to pass a string argument then a random object name will be used. This function returns a table with the below functions:

:RegisterTimedFunction (function [, wait [, repeat [, arguments]]]);

  • Registers a function to be fired on the optional wait time, that can be optionally repeated multiple times, with optional arguments. 'function' must be a function, it is preferable to register functions that are local in nature to your addon. As soon as you register a function it starts the timer object so there is no need to start it as it is already running. RegisterTimedFunction returns two methods that pause the current timer object, return the state of the current timer object, or restart it, these are:

:pause (["state"]);

  • Pauses the current timed function. If you provide the :pause() functions with the optional string "state" it will return the current state of the function. Use this to check whether the function has been paused or not either by it having expired its repeats, or because you paused it at some point.

:restart ([ wait [, repeat [, arguments]]]);

  • Restarts a paused function, or a function that has finished its repeat cycle. The optional arguments 'wait' and 'repeat' are used to change the respective options. If you do not include these then the original values you used when you first registered the function are used.
  • 'wait' must be a positive number from '0.100' and higher, note that integer values represent seconds while decimal values represent hundredths of seconds. So for example 0.100 is 100ths of a second and 60 is 60 seconds or 1 minute. The default wait time is 1 second.
  • 'repeat' must be a positive integer value of 1 or higher, negative numbers larger then -1 are converted to positive integers while decimal values represent infinity as does -1. -1 is default.
  • 'arguments' can be of any type, but be aware that they are static and do not change from the time of registration. You MUST include 'wait', and 'repeat' if you want to pass any arguments to your function.

:RemoveTimedFunction (object);

  • Removes all registered functions from the timer if 'object' is a function, or if 'object' is the returned object from 'RegisterTimedFunction' then it removes only that timer object. 'object' must be the function you registered with RegisterTimedFunction, or the object returned from RegisterTimedFunction. RemoveTimedFunction returns 'true' if success or 'false' otherwise.

[Examples]

local huh = "what?";
local function DoSomething(...)
     for i=1,10 do
          DEFAULT_CHAT_FRAME:AddMessage("Doing something this many bloody times now "..tostring(i));
     end
end

local tf = TimedFunctions:new("UberSkillzAddon");
local timer1 = tf:RegisterTimedFunction(DoSomething, 5);		-- do every 5 seconds
local timer2 = tf:RegisterTimedFunction(DoSomething, 0.500, 2);		-- do every half second and repeat 2 times

if (timer2:pause("state")) then print("I'm paused"); end               -- Checks the state of the timer

local timer3 = tf:RegisterTimedFunction(DoSomething, 60, -1, "blah", {"blah"}, huh); -- do every minute, repeat forever and use these 3 arguments
timer3:pause();                                                         -- pauses the timer

tf:RemoveTimedFunction(DoSomething);			    	        -- Remove all of the functions 'DoSomething' in the timer instance "UberSkillzAddon"

tf:RemoveTimedFunction(timer1);                                         -- Removes timer1 object from the timer instance "UberSkillzAddon"

You must login to post a comment. Don't have an account? Register to get one!

Facts

Date created
08 Jul 2009
Categories
Last update
23 Jul 2009
Development stage
Release
Language
  • deDE
  • enUS
License
GNU Affero General Public License version 3 (AGPLv3)
Curse link
Timed Functions
Recent files

Authors