Timers

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.
Post Reply
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

Timers

Post by Le_juiceBOX »

I very confused about timers. Whenever i try something it fails, could someone give me a script and explain it please?


thanks.
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Timers

Post by Sheepolution »

Are you trying to use love.timer to get some sort of clock going? That's not what love.timer is for.

If you want a timer you can do something like this:

Code: Select all

function love.load()
	time = 0
	timeLimit = 4
end


function love.update(dt)
	time = time + dt
	if time >= timeLimit then
		--Stuff happens
		time = 0 --optional if you want it to happen repeatedly
	end
end
If this is not what you meant, please elaborate on your question.
Also, check out the library tick.
Last edited by Sheepolution on Tue Jun 21, 2016 7:27 am, edited 2 times in total.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Timers

Post by s-ol »

Sheepolution wrote:Are you trying to use love.timer to get some sort of clock going? That's not what love.timer is for.

If you want a timer you can do something like this:

Code: Select all

function love.load()
	time = 0
	timeLimit = 4
end


function love.update(dt)
	time = time + dt
	if time >= timeLimit then
		--Stuff happens
		time = 0 --optional if you want it to happen repeatedly
	end
end
If this is not what you meant, please elaborate on your question.
Also, check out the library tick.
One thing I usually recommend is to, instead of counting from 0 to a hard limit, to count from an initial value to 0. That way you never need to store the limit seperately:

Code: Select all

timer = 4

function love.update(dt)
  time = time - dt
  if time <= 0 then
    -- do stuff
    time = 4 -- if you want to restart
  end
end

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Timers

Post by davisdude »

Well if you want it to repeat you would need to store that value separately, so it's pretty much the same in that case.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Timers

Post by Inny »

s-ol wrote:

Code: Select all

timer = 4

function love.update(dt)
  time = time - dt
  if time <= 0 then
    -- do stuff
    time = 4 -- if you want to restart
  end
end
If you were trying to accurately time something to happen every 4 seconds, this clock would rapidly lose milliseconds. For instance, 60FPS is a dt value of rougly 0.016, which would be 240 ticks of this clock before it fired. However you rarely get an even 60fps and will periodically see a dropped frame here and there. One easy source of dropped frames, for instance, is the player resizing their window. So, on the last frame before the timer fires, your timer variable will be 0.01. Subtract 0.016 from it and you have -0.015, which you lose by setting the new timer to 4. After several minutes if this, your clock will be off by a second. If you were trying to keep time with your game's music (because it's a music-based shooter, or a guitar-hero/frets on fire clone), your game is unplayable.

The easy easy fix, is to ADD the new timer value, rather than SET it.

Code: Select all

function love.update(dt)
  time = time - dt
  if time <= 0 then
    time = time + 4 -- This fix is to add.
  end
end
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Timers

Post by Jasoco »

Just use a timer library like Cron. It makes it so much easier and painless.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests