Need Help With love.timer.sleep

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

Need Help With love.timer.sleep

Post by Kookerus »

I'm trying to print some text, call 'love.timer.sleep', print some more text, sleep again, print more text etc.
The problem is, instead of sleeping one second and printing the text, it sleeps 3 seconds and then prints all the text at once. How do I fix this?
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Need Help With love.timer.sleep

Post by Kingdaro »

Are you using the print() function, or the love.graphics.print() function?
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: Need Help With love.timer.sleep

Post by Kookerus »

I was using 'love.graphics.print', but after trying just 'print' it works, but prints to the console, which I don't want
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Need Help With love.timer.sleep

Post by micha »

Any changes on screen are only shown, when "love.graphics.present()" is called. This is done automatically after each "love.draw()". So if you have multiple print statements in one draw, then all the changes will be done, but not be visible.

I recommend against using love.timer.sleep. Please read the other thread to a cleaner solution. There is also a blog-entry about time. Rule of thumb: Only use love.timer.sleep if you really know what you are doing.

Long story short, instead of using sleep, better try it like this:

Code: Select all

function love.load()
  timer = 0
end

function love.update(dt)
  timer = timer + dt
end

function love.draw()
  if timer > 3 then
    love.graphics.print('Text after 3 seconds', 100,100)
  end
  if timer > 6 then
    love.graphics.print('Text after 6 seconds', 100,120)
  end
end
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: Need Help With love.timer.sleep

Post by pielago »

This is how i use timer in a way ...
Hope it helps how to print on screen!

Code: Select all

local g=love.graphics
g.setBackgroundColor(100,100,100,255)
local s=0
local time = 05 
local white={255,255,255}
local red={255,0,0}

function love.update( dt )
	time = time - dt 
	
	if time < 0 then
		time= 0
		s= 1
	end
end

function love.draw()
local minutes = math.floor( time / 60 ) 
local seconds = math.floor( time % 60 ) 
	g.setColor(white)
	g.print( string.format("%02d:%02d",minutes,seconds), 400, 300 )
	
	if s==1 then
		g.setColor(red)
		g.print("Clock Stop", 400, 320 )--print to screen!
				
	end
end
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Need Help With love.timer.sleep

Post by Jasoco »

You seem to have created two threads for the same issue. So I'll repost my same reply here:

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: Need Help With love.timer.sleep

Post by Kookerus »

Jasoco wrote:You seem to have created two threads for the same issue. So I'll repost my same reply here:

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
When I use your code, nothing appears on screen.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Need Help With love.timer.sleep

Post by Jasoco »

Kookerus wrote:
Jasoco wrote:You seem to have created two threads for the same issue. So I'll repost my same reply here:

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
When I use your code, nothing appears on screen.
It's pseudocode. It's an example. Of course it's not drawing anything because you didn't put anything in love.draw().
Post Reply

Who is online

Users browsing this forum: No registered users and 209 guests