Very simple question on "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
Chaokid9
Prole
Posts: 1
Joined: Tue May 22, 2012 9:05 pm

Very simple question on "Wait".

Post 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?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Very simple question on "Wait".

Post 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?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Very simple question on "Wait".

Post 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).
10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: Very simple question on "Wait".

Post 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.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Very simple question on "Wait".

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests