Page 1 of 1

Very simple question on "Wait".

Posted: Tue May 22, 2012 9:11 pm
by Chaokid9
I used to program on a game called Roblox(Not very good engine) and it used Lua. It had something called "Wait()" It would wait for the amount of seconds in the parameters. Does LOVE have anything like this? Say I wanted to make a box, then one more, then another all one second apart. could I do this with a loop that runs every second?

Re: Very simple question on "Wait".

Posted: Tue May 22, 2012 9:50 pm
by Nixola
Yes, LÖVE has love.timer.sleep(seconds), but it's not like LÖVE works. To create 10 boxes every 5 seconds you'd have to do:

Code: Select all

function love.load()
    timer = 0
    interval = 5
    times = 10
end

function love.update(dt)
    timer = timer + dt
    if timer >= interval and times > 0 then
        createBox()
        times = times - 1
        timer = timer - interval
    end
end
If you used love.timer.sleep, you'd literally freeze the entire program and it wouldn't do anything until sleep time ends.
Do you know LÖVE callbacks and how it works?

Re: Very simple question on "Wait".

Posted: Tue May 22, 2012 10:56 pm
by Inny
This would be an interesting use case for coroutines. It's a bit of an advanced subject, but the idea goes like so:

Code: Select all

function love.load()
  graphics = {}
  game_thread = coroutine.wrap( game_runner )
end

function love.draw()
  -- space filler, draw your stuff here.
  for _, g in pairs(graphics) do draw(g) end
end

function love.update(dt)
  game_thread(dt)
end

function wait( time )
  repeat
    time = time - coroutine.yield(true)
  until time <= 0
end

function game_runner()
  graphics.boy = sprite(10, 100)
  graphics.girl = sprite(200, 100)

  for x = 10, 190 do
    graphics.boy.x = x
    wait( 0.0333 )
  end
  wait(5)
  graphics.heart = sprite( 200, 90 )
end
It's a little clunky in setting up, but the core of it is that you have a second thread of execution going that doesn't have to fall into love's event loop. As long as you resume in in love.update and yield from it without performing too much work, then things should go smooth (at least in principle).

Re: Very simple question on "Wait".

Posted: Wed May 23, 2012 2:07 am
by 10$man
Wait a sec... im I missing something? Drawing one more box every second is not a hard concept or program....

Here's some psuedo code:

Create table for images

load image

The love.update is here

Wait one second then set the total number of entries of the image table plus one to the image.

love.draw is here
Loop through the images and print each one.


I would have gave the actual code, but Im on my ipod.

Re: Very simple question on "Wait".

Posted: Thu May 24, 2012 1:52 pm
by Kadoba
Here's one way to do it.

Code: Select all

wait_time = 0

function wait(seconds)
   wait_time = wait_time + seconds
end

function love.update(dt)

   if wait_time > 0 then
      wait_time = wait_time - dt
      if wait_time > 0 then return end
      dt = -wait_time
      wait_time = 0
    end

   -- Update stuff

end