Page 1 of 2

Waiting.

Posted: Tue Apr 12, 2011 1:32 pm
by Anxiety
Im not waiting for anything, i want to know how can i wait in my code?

I have seen some versions of Lua have a 'wait()' function. Does Love have one? Incase it does have, i have really doen my code bad.
But anyways, how can i wait?

Ex:

mybool = true
wait(5)
mybool = false

Thanks!

Re: Waiting.

Posted: Tue Apr 12, 2011 1:38 pm
by akima
See this page here buddy: http://lua-users.org/wiki/SleepFunction
The author provides this solution:

Code: Select all

local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
end
-- warning: clock can eventually wrap around for sufficiently large n
-- (whose value is platform dependent).  Even for n == 1, clock() - t0
-- might become negative on the second that clock wraps.
The os.clock function is explained here: http://www.lua.org/manual/5.1/manual.html#pdf-os.clock

-Akima

Re: Waiting.

Posted: Tue Apr 12, 2011 1:42 pm
by Anxiety
Okay...

Then is it possibel to wait until a sound has finished playing? Its a static sound.

Re: Waiting.

Posted: Tue Apr 12, 2011 1:52 pm
by akima
Well. You could use the TEsound library: http://love2d.org/wiki/TEsound

That page explains that you play a sound using this function call:

Code: Select all

TEsound.play(sound, tags, volume, pitch, func)
that last parameter func, is a callback function that gets called when the sound finishes playing, so you would make a new function and pass its name as a parameter, like so:

Code: Select all

function my_func_to_call_when_sound_stops()
   -- do stuff here
end

TEsound.play("sounds/jump.ogg", {"action", "player"}, 1, 1, my_func_to_call_when_sound_stops)

Re: Waiting.

Posted: Tue Apr 12, 2011 2:05 pm
by vrld
love.timer.sleep. But what are you really trying to do?

Re: Waiting.

Posted: Tue Apr 12, 2011 3:16 pm
by Anxiety
Im trying to make a virtual weapon. I need to wait reloading.

Re: Waiting.

Posted: Tue Apr 12, 2011 3:19 pm
by vrld
Then you need a timer, because sleep will stop the execution of the whole game. How that works was explained to you before: Waiting, calling a function and waiting again

Re: Waiting.

Posted: Tue Apr 12, 2011 3:26 pm
by Robin
You don't want to sleep here, because that pauses the game completely. What you want is update the variable in another frame. You can do it like this:

Code: Select all

function love.update(dt)
  if start_reloading then -- or however you do this
    start_reloading = false
    reload_wait = 1
    mybool = true
  end
  if reload_wait then
     reload_wait = reload_wait - dt
     if reload_wait < 0 then
       mybool = false
       reload_wait = nil
     end
  end
end

Re: Waiting.

Posted: Tue Apr 12, 2011 9:10 pm
by akima
vrld wrote:Then you need a timer, because sleep will stop the execution of the whole game. How that works was explained to you before: Waiting, calling a function and waiting again
It took me quite a while to be able to picture in my head how an event loop worked and how that affected my programs. I can see why it might be hard for him to grasp right now.

Re: Waiting.

Posted: Wed Apr 13, 2011 10:22 pm
by miko
akima wrote:See this page here buddy: http://lua-users.org/wiki/SleepFunction
The author provides this solution:

Code: Select all

local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
end
-- warning: clock can eventually wrap around for sufficiently large n
-- (whose value is platform dependent).  Even for n == 1, clock() - t0
-- might become negative on the second that clock wraps.
The os.clock function is explained here: http://www.lua.org/manual/5.1/manual.html#pdf-os.clock

-Akima
This busy loop will kill your CPU. If you really want to pause the code execution, you could use socket.sleep().
You may use it in plain old lua, but you never should use it in Love - you want your game to be drawn at 60 FPS, and respond to events like keypresses or mouse clicks, right?