Waiting.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Waiting.

Post 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!
I can't come up with a good signature!
User avatar
akima
Prole
Posts: 13
Joined: Thu Mar 03, 2011 10:20 am

Re: Waiting.

Post 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
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Waiting.

Post by Anxiety »

Okay...

Then is it possibel to wait until a sound has finished playing? Its a static sound.
I can't come up with a good signature!
User avatar
akima
Prole
Posts: 13
Joined: Thu Mar 03, 2011 10:20 am

Re: Waiting.

Post 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)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Waiting.

Post by vrld »

love.timer.sleep. But what are you really trying to do?
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Waiting.

Post by Anxiety »

Im trying to make a virtual weapon. I need to wait reloading.
I can't come up with a good signature!
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Waiting.

Post 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Waiting.

Post 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
Help us help you: attach a .love.
User avatar
akima
Prole
Posts: 13
Joined: Thu Mar 03, 2011 10:20 am

Re: Waiting.

Post 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.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Waiting.

Post 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?
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests