love.timer.sleep or wait()

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
Neptoune
Prole
Posts: 1
Joined: Thu Aug 29, 2013 2:50 pm

love.timer.sleep or wait()

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.timer.sleep or wait()

Post 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
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: love.timer.sleep or wait()

Post 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.
Your screen is very zoomed in...
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests