Page 1 of 3
How would I create a timer?
Posted: Fri Dec 23, 2011 5:40 am
by All0utWar
Okay so here is what I put in love.draw:
love.graphics.printf('You have survived for' ..getDelta().. ' !', 0, 215, 620, 'center')
Here is what I put in love.update(dt):
dt = love.timer.getDelta()
Then, when I run the game I get an error that says, 'attempt to call global 'getDelta' (a nil value)' How do I do this?
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:00 am
by Jasoco
dt is already defined as a global. You don't have to define it again.
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:02 am
by All0utWar
Jasoco wrote:dt is already defined as a global. You don't have to define it again.
Oh okay, do you know how to make a timer using getDelta though?
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:12 am
by Jasoco
Edit: Here's a Helper Library with a Timer in it you can check out on the Wiki:
http://love2d.org/wiki/hump
-- Original post follows --
The basics are as follows:
(All variable names are arbitrary and just for sample purposes. This is just to show off the logic of how it works.)
Code: Select all
--First you define the timer.
timerStart = love.timer.getTime()
--In your update, you use something like this:
if love.timer.getTime() - timerStart >= timerCountdown then
--DO YOUR THING
end
That's the gist of it. I think someone might have created a library.
Alternatively:
Code: Select all
--First you define the counter.
counter = 0
--In your update, you use something like this:
counter = counter + dt
if counter >= counterLimit then
--DO YOUR THING
end
I'm sure someone has created a library. Maybe they can post it. I haven't checked the Wiki.
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:21 am
by tentus
From the example you used, I would look at
love.timer.getTime before using
love.timer.getDelta.
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:30 am
by All0utWar
I've looked at that already and I don't understand where it goes or how it is done.
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:31 am
by Jasoco
getDelta doesn't need to be called unless you're trying to use Delta in a function that doesn't have the dt variable available to it. By defauly, dt is passed into update() and you can pass it on to any other functions inside that function if you have to. But if you happen to NEED to use it elsewhere like in draw() or one of its children, you should call it.
Personally, and I've said it once before, I use my own modified version of love.run() which makes dt global instead of local so you don't have to pass it anymore. But that's just me. I'm unconventional.
Just use HUMP or play around with my coded methods.
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:55 am
by All0utWar
Oh wow, actually that was super easy to figure out after I looked at it for 5 minutes.
EDIT: How would I make it so that after the game ends, it shows the time? Everytime I do it I get 0 seconds.
http://pastebin.com/38CabixA
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 7:58 am
by Robin
You set both a and b each frame. You'll want to set b in love.load only and a in love.update. Do you see why?
Re: How would I create a timer?
Posted: Fri Dec 23, 2011 6:12 pm
by All0utWar
Robin wrote:You set both a and b each frame. You'll want to set b in love.load only and a in love.update. Do you see why?
Yeah now I understand, thanks!
EDIT: Okay, I understand how to get the timer working, but how would I show the time at the end? I tried to do these things and it won't work:
love.graphics.printf('You survived for ' .. love.timer.getMicro() .. ' seconds!', 0, 215, 620, 'center')
love.graphics.printf('You survived ' .. love.timer.getMicro(a-b) .. ' seconds!', 0, 215, 620, 'center')
love.graphics.printf('You survived for ' .. getMicroTime(a-b) .. ' seconds!', 0, 215, 620, 'center')
love.graphics.printf('You survived for ' .. getMicroTime() .. ' seconds!', 0, 215, 620, 'center')