Class: TimerTask

LuxModule: luxinialuacore

A TimerTask is a function that is executed at a certain point of time. When you create a new task, you specify a time in the future at which the function should be executed. This class also offers a very simple system of scheduling for a little loadbalancing.

TimerTasks are useful when you want to execute certain actions over a timer of at a point of time. For example, when an enemy is killed, you can create a timertask function that will remove the enmey after some time - or you can check then if a certain condition is set and react on that.

Samples:

 TimerTask.new(function () print "time over" end, 1000)
  -- prints "time over" in about a second

TimerTask.new(function () print "next frame" end) -- prints "next frame" in the next frame

TimerTask.new( function () print "from now on 30 secs" coroutine.yield(15000) print "... 15 seconds" coroutine.yield(5000) for i=10,1,-1 do print (i) coroutine.yield(1000) end print "bang" end,1 ) -- a countdown that starts in a millisecond

Methods:

Method overview:


new (function f, [int time, [int variance, [int weight, [boolean absolute] ] ] ])
returns: ([TimerTask])

Creates a new Task and returns it if the task is scheduled. The time is measured in milliseconds.

The function will be called when the current time is larger or same as the time that was used for scheduling, which is as close as possible. Be aware that a single frame may use 3-5 ms, so multiple tasks with different schedule times may be called within the same frame!

If no time is passed, the function will be called on the beginning of the next frame.

You can specify a variance in which the function should be called. The function is then scheduled between time and time+variance. You can specify a weight of that task and the scheduling will try to place the task at a time where the totalweight is at a minimum. The algorithm is pretty primitive and no rescheduling is done here.

If absolute is true, the moment of time is absolute, otherwise it is relative in the future.

A taskobject will become garbage once its scheduling time lies in the past - it is like an entry in a "calendar" which becomes unimportant once the date passed.

Once a task is scheduled, you cannot change the point of time when it will be executed.

Note: The timertasks create coroutines that are then executed. You can yield a function that is called as timertask at any time, which means that the execution is continued later. If you pass no argument to the yield call (see coroutine.yield), the execution at this point is continued at the next frame. If you pass a number, the execution is continued at this point of time in the future.

deleteAll ()
returns: ()
deletes all Tasks that are scheduled.
disable ()
returns: ()
disables a task. It won't be executed anymore.
getTaskTime ()
returns: (int time)
The TimerTask scheduling is using a own time (which can be paused). The returned time is the number of milliseconds that have passed since luxinia started minus the time when the scheduling was started. If you pass an absolute time, you have to calculate the time with this time.
isValid ()
returns: (boolean)
returns true if the task is still scheduled in the future
pauseAll (boolean pause)
returns: ()
Pauses the TimerTask scheduler. When scheduling is paused, no functions will be executed. If pause is is false, the scheduling is continued at the point when the system was paused.
reenable ()
returns: ()
reenable the task. This is senseless once the date lies in the past.
tick ()
returns: ()
called from the timer each frame.
when ()
returns: (int time)
Returns absolute moment of time when the function is called.