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?
How Do I Make A Program Pause Briefly?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How Do I Make A Program Pause Briefly?
Maybe you can use this: http://www.love2d.org/wiki/love.timer.sleep
Re: How Do I Make A Program Pause Briefly?
I was looking in 'love.timer' but I didn't even see that, lol.
google also returned a way to do it in lua
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?
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:
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.
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
Check out my blog on gamedev
Re: How Do I Make A Program Pause Briefly?
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.
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: How Do I Make A Program Pause Briefly?
Thanks for the suggestion, but I think I'm using it wrong. My code is
Am I doing something wrong?
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
Re: How Do I Make A Program Pause Briefly?
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)
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
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: How Do I Make A Program Pause Briefly?
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:
Cron 2.0 thread
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
Re: How Do I Make A Program Pause Briefly?
Is there a way I could put that in a function so I can change the value of time?
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: How Do I Make A Program Pause Briefly?
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests