Attempting to implement a wait function in love.update

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
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Attempting to implement a wait function in love.update

Post by icekiller8002 »

Hi there, see title.

I want to make something that prints "asd" every second. I am using love.update to do this, below is my code.

Code: Select all

local dt = 0

function love.update(...)
  wait(1)
  print("asd")
end

function wait(i)
  dt = dt + 1
  
  if dt >= love.timer.getFPS() * i then
    -- what do i do from here?
  end
end
I feel like this is a good start; however, I don't know if it's possible to pause the code in order to wait on a function. How would I do this? Thanks in advance.

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Attempting to implement a wait function in love.update

Post by sphyrth »

There are several ways of doing it. Here's a rudimentary one:

Code: Select all

function love.load()
  wait = 0
end

-- You update has a dt parameter
function love.update(dt)
  wait = wait + dt
  if wait >= 1 then
     wait = wait - 1
    print('asd')
  end
end
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Attempting to implement a wait function in love.update

Post by ivan »

Like Sphyrth said that's the way to do it. Note that there is no guarantee that this will run EXACTLY every second because dt varies a little bit between frames. That's why I recommend using a "fixed" timestep.
User avatar
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Re: Attempting to implement a wait function in love.update

Post by icekiller8002 »

Thank you sphyrth, I am aware of how that's one way to do it. I was wondering if there was a way to make a wait() function that pauses the update function. Is this even possible in Love2D?

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Attempting to implement a wait function in love.update

Post by grump »

"Pausing a function" is not a thing. You can either sleep, which will prevent execution of the rest of the game while sleeping.

Code: Select all

love.timer.sleep(1)
Or you can implement custom logic to make sure the parts you want to pause are not being updated while paused, as shown in the previous comments.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Attempting to implement a wait function in love.update

Post by zorg »

And chances are high that you do not want to sleep the main 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: Bing [Bot] and 24 guests