How Do I Make A Program Pause Briefly?

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
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

How Do I Make A Program Pause Briefly?

Post 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?
User avatar
ac3raven
Citizen
Posts: 60
Joined: Tue May 19, 2009 1:14 am

Re: How Do I Make A Program Pause Briefly?

Post by ac3raven »

Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How Do I Make A Program Pause Briefly?

Post 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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How Do I Make A Program Pause Briefly?

Post 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How Do I Make A Program Pause Briefly?

Post 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.
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How Do I Make A Program Pause Briefly?

Post 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?
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: How Do I Make A Program Pause Briefly?

Post 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

User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How Do I Make A Program Pause Briefly?

Post 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
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How Do I Make A Program Pause Briefly?

Post by Kookerus »

Is there a way I could put that in a function so I can change the value of time?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How Do I Make A Program Pause Briefly?

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

Who is online

Users browsing this forum: Ahrefs [Bot] and 162 guests