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.
TimerTask.new(function () print "time over" end, 1000) -- prints "time over" in about a secondTimerTask.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
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.