Page 1 of 1

How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 3:39 am
by Kookerus
I wanted to have some text appear on screen, wait a second or two, and then have more appear. In python you would use 'time.sleep(seconds)'
Is there a way to accomplish this in LÖVE or Lua?

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 3:47 am
by ac3raven

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 3:50 am
by Kookerus
I was looking in 'love.timer' but I didn't even see that, lol.
google also returned a way to do it in lua

Code: Select all

function sleep(n)
  os.execute("sleep " .. tonumber(n))
end

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 6:41 am
by micha
I personally would not use the love.timer.sleep function, because it makes the game unresponsive. LÖVE games consist of a main loop and when you use sleep, then the main loop is interrupted. That means that the user cannot quit the game in this moment. And if you have menus for example, you cannot open any menu during a sleep.

The cleaner solution is to keep the main-loop running, but to not update anything in this time. Something like this:

Code: Select all

function sleep(time)
  sleeping = true
  sleepTimer = time
end

function love.update(dt)
  if sleeping then
    sleepTimer = sleepTimer - dt
    if sleepTimer < 0 then sleeping = false end
    return
  end
  -- put the normal update stuff here
end
It is a bit of extra work to figure out which parts have to pause during the "sleep" and which parts have to stay active (for example menu button), but it is worth it, because the game should always stay responsive.

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 6:47 am
by T-Bone
Just wanted to add a little to what Micha wrote. love.timer.sleep can still be useful, but it should mostly be used in other threads, were the game's main thread can still be running and the game will stay responsive.

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 6:53 am
by Kookerus
Thanks for the suggestion, but I think I'm using it wrong. My code is

Code: Select all

function sleep(time)
  sleeping = true
  sleepTimer = time
end

function love.update(dt)
  if sleeping then
    sleepTimer = sleepTimer - dt
    if sleepTimer < 0 then sleeping = false end
    return
  end
  -- put the normal update stuff here
end

function love.draw()
    love.graphics.print("Hello!", 10, 10)
    sleep(1)
    if sleeping == true then
        love.graphics.print("World!", 10, 20)
    end
end
Am I doing something wrong?

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 11:24 am
by pielago
I am not sure if what you want is to end it or loop it?
if you want loop you can set it under update(dt)

Code: Select all

local time = 05
local sleeping = false
local sleepTimer = time
function love.update(dt)
    sleepTimer = sleepTimer - dt
	
    if sleepTimer < 0 then  		
		sleeping = true
      sleepTimer = 0 --kill it or loop it!
	end    
end

function love.draw()
    love.graphics.print("Hello!", 10, 10)

    if sleeping == true then
        love.graphics.print("World!", 10, 20)
    end
end


Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 7:00 pm
by Jasoco
Use a Timer library like Cron. Don't use love.timer.sleep or os.execute. They don't do what you think they do.

If you use cron, it would be as easy as:

Code: Select all

message = {
    text = "This is my message",
    visible = true
}

local seconds = 2 --a second or two
message.timer = cron.after(seconds, function() message.visible = false end)

function yourDrawingFunction()
    if message.visible then
        love.graphics.print(message.text, 10, 10)
    end
end
Cron 2.0 thread

Re: How Do I Make A Program Pause Briefly?

Posted: Wed Jul 30, 2014 7:06 pm
by Kookerus
Is there a way I could put that in a function so I can change the value of time?

Re: How Do I Make A Program Pause Briefly?

Posted: Thu Jul 31, 2014 12:06 am
by Jasoco
There's a way to do anything. It's just pseudocode. I'm just showing you how Cron works. It handles timers for you. You'd have to offer us more information if you want more help. Upload a .love that you're having trouble with.