Hourglass - Basic Timer Library v0.7!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Maggots
Prole
Posts: 27
Joined: Sat Apr 24, 2010 10:38 pm
Location: Montréal, Québec, Canada

Hourglass - Basic Timer Library v0.7!

Post by Maggots »

I had to add timers to a little project of mine, so I made a small library for basic timers. More pro (as in more than me) coders won't need this at all, and there's probably already a lib for this, but I just thought I'd share. Also, I gave it a name. It's (because of Luiji) released under the ZLib license.

It's really not complicated though. Call me a nub. I dare you. :(

Docs:

Code: Select all

--Functions
hourglass:updateAll(dt) --Updates all the timers.
hourglass:getAll() --Returns all timers
hourglass:resetAll() --Restarts all timers
hourglass:pauseAll(boolean) --Pauses all timers.
hourglass:destroyAll() --Destroys all the timers.
---------------------------------------------------------
hourglass.timer:new(delay, function) --Creates a timer with a set delay in milliseconds and a function that will be called.
hourglass.timer:update(dt) --Updates the timer's clock. Returns true and calls its set function if it triggers.
hourglass.timer:pause(boolean) --Pauses the timer.
hourglass.timer:reset() --Restarts the timer
hourglass.timer:destroy() --Destroys the timer.
hourglass.timer:getClock() --Returns the amount of time elapsed.
hourglass.timer:getDelay() --Returns the timer's delay.
hourglass.timer:isTriggered() --Returns if the timer is triggered or not.
hourglass.timer:isPaused() --Returns if the timer is paused or not.

Code: Select all

--Example for 0.5 (uses classes)
hourglass = require 'hourglass.lua'

function love.load()
	text = 'D:'
	a = hourglass.timer:new(1000, function() text=':D' end)
	a:pause(true)
	b = hourglass.timer:new(500, function() a:pause(false) end)
end

function love.draw()
	love.graphics.print(text, 400, 300)
end

function love.update(dt)
	hourglass:update(dt)
end
Mind that this uses milliseconds. ;)

Update 06/07/10:
-Changed function names
-Added hourglass.timer:reset() and hourglass:resetAll()
Attachments
hourglass.lua
v0.7
(2.31 KiB) Downloaded 279 times
Last edited by Maggots on Wed Jul 07, 2010 2:01 am, edited 15 times in total.
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: Hourglass - small timer library

Post by Luiji »

Cool. I may integrate this into Boxdude. What's the license? (Please make it something GPL-compatable, CC-BY isn't.)
Good bye.
User avatar
Maggots
Prole
Posts: 27
Joined: Sat Apr 24, 2010 10:38 pm
Location: Montréal, Québec, Canada

Re: Hourglass - small timer library

Post by Maggots »

I don't know much about licenses D:

I guess I'll give it the same license that Love uses: the zlib license.

Ruling declared. *slams mallet*
User avatar
Elvashi
Prole
Posts: 45
Joined: Sat Jul 04, 2009 9:17 am
Location: Australia

Re: Hourglass - small timer library

Post by Elvashi »

I'd suggest using the functions in love.timer.*, rather than os.*. other than that, looks fine.
"We could make a program for doing this for you, but that is for the LÖVE IDE, planned to be released in March 2142." ~mike
Networking with UDP uLove Proposal CCG: Gangrene
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Hourglass - small timer library

Post by vrld »

Cool would be something like this:

Code: Select all

hourglass.timer(1000, function() do_stuff() end)
i.e. a delayed function call. Please? :3
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Maggots
Prole
Posts: 27
Joined: Sat Apr 24, 2010 10:38 pm
Location: Montréal, Québec, Canada

Re: Hourglass - small timer library

Post by Maggots »

Aw, sure :3

I just woke up, so I'll add that stuff now.

Edit:
Done and done.

Edit 2:
Elvashi wrote:I'd suggest using the functions in love.timer.*, rather than os.*. other than that, looks fine.
Well, I use os.clock() so that it works for both Love and normal Lua. ;)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Hourglass - small timer library

Post by kikito »

You might want to be interested in having a look at the passion.timer module. It includes timed execution (with passion.timer.after) as well as periodical (passion.timer.every) and effects for advanced things (passion.timer.effect).

One day I will complete the documentation for all this.
When I write def I mean function.
User avatar
Maggots
Prole
Posts: 27
Joined: Sat Apr 24, 2010 10:38 pm
Location: Montréal, Québec, Canada

Re: Hourglass - small timer library

Post by Maggots »

Interesting. It's a lot simpler than my library. I'll take a closer look at the Passion library. :awesome:
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Hourglass - small timer library

Post by Jasoco »

I have something like this in my game. I call it "setTimeout" like in JavaScript. Don't use it as much as I should though. Probably in time.. get it?
User avatar
Maggots
Prole
Posts: 27
Joined: Sat Apr 24, 2010 10:38 pm
Location: Montréal, Québec, Canada

Re: Hourglass - small timer library

Post by Maggots »

Jasoco wrote:Probably in time.. get it?
:|
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 56 guests