Page 1 of 1

Delay while running after around 20 seconds

Posted: Thu Jan 27, 2011 3:04 am
by Kantos
So I started playing around with this doing my own basic sample code and I noticed that pretty much no matter what I my scripts start to experience 2~3 second delay between input and changes after around 20 seconds of runtime.

Here is my basic sample code:

-- Globals, becuase Lua is silly like that
num = 15
time = 0
text = 'wtf'

x = 100
y = 100



--Updates
function love.update()

end

--Checks Input
function love.keypressed(k)


if k == 'left' then
time = love.timer.getTime()
end

if k == 'w' then
y = y - 5
end

if k == 's' then
y = y + 5
end

if k == 'a' then
x = x - 5
end

if k == 'd' then
x = x + 5
end

end

--Draws to scren
function love.draw()
love.graphics.setFont(love.graphics.newFont(18))
love.graphics.print('Hello World!', 400, 300)
love.graphics.print(num, 200,200)
love.graphics.print(time, 400,320)
love.graphics.print(text, x,y)
end

------

Does anyone know what I might be doing to cause this or anyway to prevent/fix this rather annoying bug I seem to be experiencing? Would greatly appreciate any help on the topic. I do not experience any such lag when running any of the demos regardless of run-time so I feel it must be something I'm doing. Thanks again!

Re: Delay while running after around 20 seconds

Posted: Thu Jan 27, 2011 3:34 am
by tentus
First of all, please use the code tags in the future, it makes it much easier to help you. Second of all, your bug was because you were creating a new font with every frame drawn. Here's my revised code:

Code: Select all

function love.load()
	love.graphics.setFont(love.graphics.newFont(18))
	num = 15
	thetime = 0
	text = 'wtf'
	x = 100
	y = 100
end

function love.update(dt)
	-- nothing, but isDown could be used here for smoother movement
end

function love.keypressed(k)
	if k == ' ' then
		thetime = love.timer.getTime()
	end
	if k == 'w' then
		y = y - 5
	end
	if k == 's' then
		y = y + 5
	end
	if k == 'a' then
		x = x - 5
	end
	if k == 'd' then
		x = x + 5
	end
end

function love.draw()
	love.graphics.print('Hello World!', 400, 300)
	love.graphics.print(num, 200,200)
	love.graphics.print(thetime, 400,320)
	love.graphics.print(text, x,y)
end
As you can see, I moved a few things into love.load(), and did some minor housekeeping. I would advise avoiding the word time, I'm pretty sure it's a reserved word. Also, you will want to start looking into dt next, it'll help you uncouple movement from keypresses.

Re: Delay while running after around 20 seconds

Posted: Thu Jan 27, 2011 6:33 am
by Robin
tentus wrote:I would advise avoiding the word time, I'm pretty sure it's a reserved word.
Nope. os.time() is a standard library function though.

Re: Delay while running after around 20 seconds

Posted: Thu Jan 27, 2011 12:27 pm
by leiradel
Kantos wrote:-- Globals, becuase Lua is silly like that
num = 15
time = 0
text = 'wtf'
Lua is not silly:

Code: Select all

-- Script locals
local num = 15
local time = 0
local text = 'wtf'
But if what you're complaining is that you have to declare variables there so they are visible to love.load, love.update and love.draw, you'd have to do the same thing if LÖVE used another script language. You'd have to do the same thing even if LÖVE was a C++ library with the same API.

But of course there are better ways to organize your code.

Cheers,

Andre