[library] cron.lua - time management for LÖVE - v2.0 is out!
Posted: Sun Apr 24, 2011 4:09 pm
EDIT: I've updated this post to reflect the status of cron 2.0
This is a time management library. It can be used on any lua application, but it's specially tailored to work with LÖVE.
https://github.com/kikito/cron.lua
Here is a basic example showing how to use cron to make a gun that is able to shot every two seconds, using a "one-time" clock that triggers an action after 2 seconds. In this case, the action is setting a variable to true. It could be much more complicated than that.
In addition to one-time events, cron.lua also allows creating clocks that automatically do execute an action (in cron's terms: invoke a callback) every x seconds. For example, this code will play a sound every 10 seconds:
You can use both anonymous functions and predefined functions. For example, the previous love.load could be written like this:
You can also add parameters to functions. So the previous function can also be written like this:
You can add as many clocks as you want, from any function you want. The only requirement it has is that you don't forget to invoke update(dt) on the clocks you are using.
I've created a small demo that can give you a better taste. The looks are very spartan - this was on purpose. I wanted to make the source code as clear as possible. Give it a look and let me know what you think!
Appart from that, on the github page there's more documentation and examples on how to use the library.
Regards!
This is a time management library. It can be used on any lua application, but it's specially tailored to work with LÖVE.
https://github.com/kikito/cron.lua
Here is a basic example showing how to use cron to make a gun that is able to shot every two seconds, using a "one-time" clock that triggers an action after 2 seconds. In this case, the action is setting a variable to true. It could be much more complicated than that.
Code: Select all
local cron = require 'cron'
local gunCoolDownClock
local gunReady = true
function fire()
if gunReady then
newBullet()
gunReady = false
end
-- next fire can happen only after two seconds
gunCoolDownClock = cron.after(2, function() gunReady = true end)
end
function love.update(dt)
-- This will activate the gun once enough time has passed
if gunCoolDownClock then gunCoolDownClock:update(dt) end
end
function love.keypress()
fire()
end
Code: Select all
local cron = require 'cron'
local alarmLoopClock
function love.load()
local sound = love.audio.newSource('beep.mp3')
alarmLoopClock = cron.every(10, function() sound:play() end)
end
function love.update(dt)
alarmLoopClock:update(dt)
end
You can use both anonymous functions and predefined functions. For example, the previous love.load could be written like this:
Code: Select all
function love.load()
local sound = love.audio.newSource('beep.mp3')
local playSound = function() sound:play() end
alarmLoopClock = cron.every(10, playSound)
end
Code: Select all
function love.load()
local sound = love.audio.newSource('beep.mp3')
alarmLoopClock = cron.every(10, sound.play, sound)
end
I've created a small demo that can give you a better taste. The looks are very spartan - this was on purpose. I wanted to make the source code as clear as possible. Give it a look and let me know what you think!
Appart from that, on the github page there's more documentation and examples on how to use the library.
Regards!