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
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

wait()

Post by Le_juiceBOX » Sat Apr 09, 2016 1:27 am

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.
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!

User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: wait()

Post by Vimm » Sat Apr 09, 2016 4:05 am

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

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: wait()

Post by Kingdaro » Sat Apr 09, 2016 4:19 am

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

User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: wait()

Post by Vimm » Sat Apr 09, 2016 4:27 am

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

User avatar
zorg
Party member
Posts: 2868
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: wait()

Post by zorg » Sat Apr 09, 2016 5:50 am

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?).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: wait()

Post by Inny » Sat Apr 09, 2016 3:33 pm

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!

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: wait()

Post by Kingdaro » Sat Apr 09, 2016 4:28 pm

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

iGottic
Prole
Posts: 1
Joined: Mon Feb 24, 2020 12:31 pm

Re: wait()

Post by iGottic » 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 :)

User avatar
zorg
Party member
Posts: 2868
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: wait()

Post by zorg » Tue Feb 25, 2020 10:26 pm

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Post Reply

Who is online

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