Page 1 of 1

wait()

Posted: Sat Apr 09, 2016 1:27 am
by Le_juiceBOX
how can i do this?

while true do
draw(face1)
wait(1)
draw(face2)
end

when ever i try to use wait() it comes up with an error please help with a script in the simplest way i can do this.

Re: wait()

Posted: Sat Apr 09, 2016 4:05 am
by Vimm
Le_juiceBOX wrote:how can i do this?

while true do
draw(face1)
wait(1)
draw(face2)
end

when ever i try to use wait() it comes up with an error please help with a script in the simplest way i can do this.
It's not recommended, but you could try

Code: Select all

while true do
	draw(face1)
	love.timer.sleep(1)
	draw(face2)
end
I'm not 100% sure that will work though, and like I said it isn't recommended. There are better ways but they require more code.

Also, as a side note, when asking for help on the forums, always include the .love file so we can see the source code and better help you

Re: wait()

Posted: Sat Apr 09, 2016 4:19 am
by Kingdaro
I wouldn't recommend using love.timer.sleep at all. It'll freeze your game for the duration of it, and it won't show the first face.

What you're probably looking for is a timer. Here's a simple way to do your example:

Code: Select all

function love.load()
  -- load face images
  face1 = love.graphics.newImage(...)
  face2 = love.graphics.newImage(...)

  -- keep track of which face we're drawing
  currentFace = face1

  -- this variable represents our timer
  faceTimer = 0
end

function love.update(dt)
  -- add to the timer every frame
  faceTimer = faceTimer + dt

  -- check if the timer hits the limit
  if faceTimer > 1 then
    -- reset the timer
    faceTimer = 0

    -- switch the face
    if currentFace == face1 then
      currentFace = face2
    else
      currentFace = face1
    end
  end
end

function love.draw()
  -- draw the current face at x 100 y 100
  love.graphics.draw(currentFace, 100, 100)
end
There are libraries that make the process a little nicer and cleaner looking, though, so you don't have to keep a bunch of timer variables and manage them yourself. A good one to look at is hump.timer, and hump comes with a lot of other goodies too. You can get it from github: https://github.com/vrld/hump

Re: wait()

Posted: Sat Apr 09, 2016 4:27 am
by Vimm
Kingdaro wrote:I wouldn't recommend using love.timer.sleep at all. It'll freeze your game for the duration of it, and it won't show the first face.

What you're probably looking for is a timer. Here's a simple way to do your example:

Code: Select all

function love.load()
  -- load face images
  face1 = love.graphics.newImage(...)
  face2 = love.graphics.newImage(...)

  -- keep track of which face we're drawing
  currentFace = face1

  -- this variable represents our timer
  faceTimer = 0
end

function love.update(dt)
  -- add to the timer every frame
  faceTimer = faceTimer + dt

  -- check if the timer hits the limit
  if faceTimer > 1 then
    -- reset the timer
    faceTimer = 0

    -- switch the face
    if currentFace == face1 then
      currentFace = face2
    else
      currentFace = face1
    end
  end
end

function love.draw()
  -- draw the current face at x 100 y 100
  love.graphics.draw(currentFace, 100, 100)
end
There are libraries that make the process a little nicer and cleaner looking, though, so you don't have to keep a bunch of timer variables and manage them yourself. A good one to look at is hump.timer, and hump comes with a lot of other goodies too. You can get it from github: https://github.com/vrld/hump

That's what I was gonna recommend, but OP said the simplest way, i figured love.timer.sleep() was the simplest, even if it is also the worst lol

Re: wait()

Posted: Sat Apr 09, 2016 5:50 am
by zorg
Technically, it's not the same, though the OP didn't really specify what he wanted to do anyway... apart from wait not existing as a function in neither lua nor löve (maybe somewhere else?).

Re: wait()

Posted: Sat Apr 09, 2016 3:33 pm
by Inny
Bare with me for a second, but you can still get your own wait function in love using coroutines.

Code: Select all

local thread = coroutine.wrap(function()
  while true do
    draw(face1)
    my_wait(1)
    draw(face2)
    my_wait(1)
  end
end)
In this scenario, whenever the thread is resumed (because I used wrap, you just call it), it'll pickup where it left off with a yield call. That's what the my_wait function will be, the yield calls and a timer.

Code: Select all

function my_wait(time)
  while time > 0 do
    time = time - coroutine.yield()
  end
end
Of course, that yield function is returning how much time has passed, which it needs to get from somewhere, which is you pass it in as an argument to the thread created by coroutine.wrap

Code: Select all

function love.update(dt)
  thread(dt)
end
As for the draw function, that's a bit more complicated which I'll hand wave like so:

Code: Select all

to_draw = nil

function draw(img)
  to_draw = img
end

function love.draw()
  -- do whatever is needed to draw the last image.
end
Hope this helps in a confusing way, or confuses in a helpful way!

Re: wait()

Posted: Sat Apr 09, 2016 4:28 pm
by Kingdaro
zorg wrote:Technically, it's not the same, though the OP didn't really specify what he wanted to do anyway... apart from wait not existing as a function in neither lua nor löve (maybe somewhere else?).
He's probably referring to ROBLOX's wait(). ROBLOX scripts all run in a coroutine. http://wiki.roblox.com/index.php?title= ... OBLOX#Wait

Re: wait()

Posted: Mon Feb 24, 2020 12:44 pm
by iGottic
Yea. It might be kinda cool to figure out how to do this in love tho.
Roblox does it by having wait() as a yield to a function, that returns time passed and delta.
However, Roblox has a different version of Lua like Kingdaro mentioned (more optimization, LuaU sidecode language, etc.)

In love, it's more complicated (more classic Lua style). In 99% of game languages, you can't just call "wait()." Efficient programming languages check code every tick, and having a wait() will interrupt the flow.

Run things off of EVENTS. An event can fire for almost anything, so give it a try :)

Re: wait()

Posted: Tue Feb 25, 2020 10:26 pm
by zorg
iGottic wrote: Mon Feb 24, 2020 12:44 pm Yea. It might be kinda cool to figure out how to do this in love tho.
Roblox does it by having wait() as a yield to a function, that returns time passed and delta.
However, Roblox has a different version of Lua like Kingdaro mentioned (more optimization, LuaU sidecode language, etc.)

In love, it's more complicated (more classic Lua style). In 99% of game languages, you can't just call "wait()." Efficient programming languages check code every tick, and having a wait() will interrupt the flow.

Run things off of EVENTS. An event can fire for almost anything, so give it a try :)
One, welcome to the forums.

Two, luaJIT that löve uses, still has corouties, and if you utilize it efficiently, you can implement wait, delay, whatever with them.

Three, next time, do check the post dates; there's not much chance the OP would see a 4 year old thread.