100. Timers (revisited)
There is an interesting forum post here, about timers, showing how tweens can be used.
Suppose we want to wait 1 second and then run the function NextStep.
The “normal” approach would be to use a counter, like this.
--somewhere in your code timer=0 --then in the draw function timer=timer + DeltaTime --add time since last redraw if timer>1 then NextStep() end
But you can use tweens to do this more elegantly
--somewhere in your code tween.delay(3,NextStep)
Tween.delay works in exactly the same way as a counter, but is more elegant.
Repeating timers
If you want a repeating timer, eg something that happens every 3 seconds, you can call tween.delay again, after it finishes, ie
--somewhere in your code tween.delay(3,NextStep) function NextStep() --all your code here, then at the end... tween.delay(3,NextStep) end
What is also nice is that if you are using a counter of your own, and testing its value in the draw function, when the counting is done, you need to turn it off so draw doesn’t keep using it. Tween handles all this for you, automatically shutting down the counter at the end.
Very neat.
It’s not going to work for everything – measuring things like frames per second (speed) is still best done with your own counters – but it is a useful tool to know about.