How would I create a timer?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
All0utWar
Prole
Posts: 17
Joined: Tue Dec 06, 2011 12:36 am

How would I create a timer?

Post 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?
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How would I create a timer?

Post by Jasoco »

dt is already defined as a global. You don't have to define it again.
User avatar
All0utWar
Prole
Posts: 17
Joined: Tue Dec 06, 2011 12:36 am

Re: How would I create a timer?

Post 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?
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How would I create a timer?

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How would I create a timer?

Post by tentus »

From the example you used, I would look at love.timer.getTime before using love.timer.getDelta.
Kurosuke needs beta testers
User avatar
All0utWar
Prole
Posts: 17
Joined: Tue Dec 06, 2011 12:36 am

Re: How would I create a timer?

Post by All0utWar »

tentus wrote:From the example you used, I would look at love.timer.getTime before using love.timer.getDelta.
I've looked at that already and I don't understand where it goes or how it is done.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How would I create a timer?

Post by Jasoco »

tentus wrote:From the example you used, I would look at love.timer.getTime before using love.timer.getDelta.
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.
User avatar
All0utWar
Prole
Posts: 17
Joined: Tue Dec 06, 2011 12:36 am

Re: How would I create a timer?

Post by All0utWar »

tentus wrote:From the example you used, I would look at love.timer.getTime before using love.timer.getDelta.
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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How would I create a timer?

Post 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?
Help us help you: attach a .love.
User avatar
All0utWar
Prole
Posts: 17
Joined: Tue Dec 06, 2011 12:36 am

Re: How would I create a timer?

Post 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')
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 3 guests