Page 1 of 1

Delay function isn't working.

Posted: Sun May 27, 2012 4:17 am
by DeadInternal
Basically what the function is suppose to do is wait the amount of seconds specified. The function is inside the love.update function. This is the first block of code. The second block of code basically plays 2 noises if you click the left mouse button. First it plays the 'fire' noise, then after 5 seconds it's suppose to play the 'shell' noise. However, no time is waited at all and they're both played at the same time.

Code: Select all

function love.update(dt)
function wait(time)
local total = 0
repeat total = total + dt
until total >= time
end
end

function love.mousepressed(mousex, mousey, button)
if button == "l" then 
fire = love.audio.newSource("src/sound/pistolfire.mp3", "static")
love.audio.play(fire)
wait(5)
--Shell dropping sound--
shell = love.audio.newSource("src/sound/pistolshell.mp3", "static")
love.audio.play(shell)
end
end

Re: Delay function isn't working.

Posted: Sun May 27, 2012 7:29 am
by bartbes
What the-

First, don't define functions inside other functions, you can, but you don't look like you know what you're doing with it.
Another thing to note, you'll never be able to build a 'synchronous' wait that doesn't completely block all other games, so you won't ever be able to use something like that inline.

Re: Delay function isn't working.

Posted: Sun May 27, 2012 3:46 pm
by DeadInternal
So is it flat-out not logically possible to create a wait? I was thinking about attempting a few experiment with coroutines next. Bleh, I'll live without the wait.

Re: Delay function isn't working.

Posted: Sun May 27, 2012 3:58 pm
by kikito
Maybe you want to give a look at cron.lua. cron.after might be what you are looking for.

Re: Delay function isn't working.

Posted: Sun May 27, 2012 4:13 pm
by DeadInternal
kikito wrote:Maybe you want to give a look at cron.lua. cron.after might be what you are looking for.

That solved it! Thank you!