Page 1 of 5

[library] cron.lua - time management for LÖVE - v2.0 is out!

Posted: Sun Apr 24, 2011 4:09 pm
by kikito
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.

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
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:

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
You can also add parameters to functions. So the previous function can also be written like this:

Code: Select all

function love.load()
  local sound = love.audio.newSource('beep.mp3')
  alarmLoopClock = cron.every(10, sound.play, sound)
end
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!

Re: cron.lua

Posted: Sun Apr 24, 2011 4:34 pm
by Ensayia
This is quite simple and has loads of practical use. Perhaps some of these libraries you are cooking up should be integrated into LOVE.

Re: cron.lua

Posted: Sun Apr 24, 2011 4:40 pm
by thelinx
Ensayia wrote:This is quite simple and has loads of practical use. Perhaps some of these libraries you are cooking up should be integrated into LOVE.
As useful as they are, third-party libraries should remain third-party.

Re: cron.lua

Posted: Sun Apr 24, 2011 6:00 pm
by vrld
Wow, you're awefully productive lately. I hope you keep up the good work!
(Though I see this one as direct rival for hump.timer. But then again, diversity is a good thing.)

Re: cron.lua

Posted: Sun Apr 24, 2011 7:56 pm
by BlackBulletIV
Rapid fire! Looks good mate. :)

Re: cron.lua

Posted: Sun Apr 24, 2011 8:42 pm
by kikito
Thanks for your kind words guys.

I had 4 days of holidays and I wanted to try TDD on Lua.

Verdict: it works.
Ensayia wrote:This is quite simple and has loads of practical use. Perhaps some of these libraries you are cooking up should be integrated into LOVE.
Actually, I'm favoring smaller libraries these days. It's not something dogmatic; empirically, to me, smaller libs work better than bigger ones (more on that below). The trick is making them simple to install, and providing the right amount of documentation.

But you have reminded me that I have to include my libs on the libraries wiki page. Thanks!
vrld wrote:(Though I see this one as direct rival for hump.timer. But then again, diversity is a good thing.)
Let me tell you a secret: memoize.lua, inspect.lua and cron.lua are all descendants of partst of my old PÄSSION lib. The first two were inside the passion module itself, implemented as half-baked functions with no tests. cron's ancestor was passion.timer.

PÄSSION got big so fast that it was very painful to maintain - to the point I didn't enjoy hacking with it any more. By separating it in smaller, focused libs, I'm having fun again. And the quality of code is much higher.

Another challenge I've found is that, if I want my libs to be as usable as possible, I can't depend on middleclas. So back to the basics, no OOP for you! It's a challenge, but it is refreshing.

Re: cron.lua

Posted: Sun Apr 24, 2011 9:12 pm
by BlackBulletIV
kikito wrote:PÄSSION got big so fast that it was very painful to maintain - to the point I didn't enjoy hacking with it any more.
Deja vu... :roll: (speaking of my, now dead, monolithic failure, Grace - for those who don't know)

EDIT: Also:
kikito wrote:PS: No metatables were harmed during the development of this library.
Ha ha! Is it because of the metatable magic performed in MiddleClass that you say that?

Re: cron.lua

Posted: Sun Apr 24, 2011 9:25 pm
by kikito
BlackBulletIV wrote:
kikito wrote:PS: No metatables were harmed during the development of this library.
Ha ha! Is it because of the metatable magic performed in MiddleClass that you say that?
:) No, this was (yet another) wink to Taehl.

Re: cron.lua

Posted: Sun Apr 24, 2011 10:38 pm
by BlackBulletIV
Ah yes, of course.

Re: cron.lua

Posted: Mon Apr 25, 2011 11:03 am
by vrld
kikito wrote:Actually, I'm favoring smaller libraries these days. It's not something dogmatic; empirically, to me, smaller libs work better than bigger ones (more on that below). The trick is making them simple to install, and providing the right amount of documentation.
Amen. Though it's not always possible (a scene manager for example).
kikito wrote:Let me tell you a secret: memoize.lua, inspect.lua and cron.lua are all descendants of partst of my old PÄSSION lib.
You're building some kind of meta engine then, providing the tools to build your own engine? ;)
kikito wrote:So back to the basics, no OOP for you!
kikito wrote:And the quality of code is much higher.
Coincidence? :ehem: taking quotes out of context is fun!