love-timer | A small timer lib

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
IS2511
Prole
Posts: 4
Joined: Fri Jun 28, 2019 3:24 pm

love-timer | A small timer lib

Post by IS2511 »

(New to the forum)

I noticed I was writing similar bits of code every time I wanted to keep track of time and so the idea came to me. This is just a small library I wrote to optimize timekeeping in my code. I'm interested if this is how OOP looks on lua and if I'm doing it right, any suggestions/comments are welcome. :з

Code: Yes, it's me, the source code

I documented the code pretty well. Or so I think. But if you are lazy still not sure how to use the library, here are some examples:

Code: Select all

local Timer = require "love-timer"

local timer1 = Timer.new()
timer1:start() -- Now it's counting up from 0 seconds

local timer2 = Timer.new(10) -- Setting the timer for 10 seconds
timer2:start()
while not timer2() do -- timer2() is the same as timer2:check()
  -- Busy loop waiting...
end
-- We are here after 10 seconds pass
print(#timer1) -- Should be somewhere around 10 seconds, #timer1 = timer1:time()

local timer3 = Timer.new(5) -- Setting the timer for 5 seconds
while #timer3 > 0 do
  -- Busy waiting again...
end
print(#timer2) -- Should be around -5 seconds because we went over 10 set seconds

timer1:start() -- We actually don't need to stop(), start() works as a reset
print(#timer1) -- Should be around 0 seconds

while #timer1 < 4 do
  -- Busy waitng for 4 seconds
end
timer1:stop()
print(#timer1) -- nil because timer is stopped
print( (Timer.new()):time() ) -- Same

timer1:set(5) -- timer1 is now a 5 second timer
timer2:set() -- timer2 is now just counting
『IS2511』
Kernel panic - not syncing: No working mind found!
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: love-timer | A small timer lib

Post by 4vZEROv »

OOP can be implemented in different ways in Lua, your way is good.
Not a fan of the deepcopy inside the constructor, why don't you initialize the variables here ?
User avatar
IS2511
Prole
Posts: 4
Joined: Fri Jun 28, 2019 3:24 pm

Re: love-timer | A small timer lib

Post by IS2511 »

4vZEROv wrote: Mon Nov 11, 2019 12:10 pm Not a fan of the deepcopy inside the constructor, why don't you initialize the variables here ?
I though about creating a table inside .new(), but I really wanted to keep functions and variables separated and I like C++ style class declaration. (My main language is C++).
Initializing inside .new() seems to be a little bit faster though.
Maybe if I somehow put a link to the original table into every object I can create static class variables :monocle: Hmm...
『IS2511』
Kernel panic - not syncing: No working mind found!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 44 guests