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

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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!
Attachments
crondemo.love
(2.25 KiB) Downloaded 546 times
Last edited by kikito on Wed Sep 25, 2013 10:34 pm, edited 3 times in total.
When I write def I mean function.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: cron.lua

Post 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.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: cron.lua

Post 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.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: cron.lua

Post 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.)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: cron.lua

Post by BlackBulletIV »

Rapid fire! Looks good mate. :)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: cron.lua

Post 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.
When I write def I mean function.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: cron.lua

Post 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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: cron.lua

Post 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.
When I write def I mean function.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: cron.lua

Post by BlackBulletIV »

Ah yes, of course.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: cron.lua

Post 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!
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests