Page 1 of 1

love.timer.sleep or wait()

Posted: Thu Aug 29, 2013 2:57 pm
by Neptoune
Just wondering if I can do something like wait()
I tried love.timer.sleep, the game just crashed.
Tried googling it, I saw the wiki page.
I got this code that increases the variable every one second:

Code: Select all

x = 0
function love.update(dt)
   x = x + dt
end
That crashes the game also and where did one second come from?

Code: Select all

function love.load()
	logo = love.graphics.newImage("logo.png")
	medium = love.graphics.newFont(45)
	num = 0
end

function love.update(dt)
	if love.keyboard.isDown("up") then
		num = num + 1
	end
	if love.keyboard.isDown("down") and num > 0 then
		num = num - 1
	end
end


function love.draw()
	love.graphics.print(num, 200, 200)
	love.graphics.setFont(medium)
end
What it does is increase the number while you hold the up arrow, decrease the number while you hold the down arrow, and stop before it goes into the negatives.
What I need it to do is have some sort of sleep() so that it waits a certain amount of time before increasing the number again.
Sorry if this is kind of a dumb question in general, I'm new to love2d.

Re: love.timer.sleep or wait()

Posted: Thu Aug 29, 2013 3:49 pm
by raidho36
Are you sure you need sleep function? That will put the whole program to a halt until sleep is timed out.

What I suggest is having another "timeout" variable that is set when you press a key, and decreased over time until reaches zero.

Code: Select all

if ( whatever ) and timeout == 0 then blah blah blah; timeout = 2.5 end
love.update ( dt )
    timeout = math.max ( 0, timeout - dt )
end

Re: love.timer.sleep or wait()

Posted: Fri Aug 30, 2013 9:28 am
by mickeyjm
The reason this:

Code: Select all

x = 0
function love.update(dt)
   x = x + dt
end
increases by 1 every second is because dt is equal to the number of seconds that past since the last frame. This means if your program is running at 100 frames per second dt is going to be 0.01 and it will take 100 updates to add 1 to the x variable.

Also love.timer.sleep freezes the whole program because when you run it everything stops until it is completed. I'm going to guess that you have learnt lua from ROBLOX where every script has its own thread and consequently it is common to pause individual scripts when you need a delay. This is not the same in LOVE as (for the most part) everything runs in the same thread so to stop one would stop them all.