Delay while running after around 20 seconds

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
Kantos
Prole
Posts: 1
Joined: Thu Jan 27, 2011 3:01 am

Delay while running after around 20 seconds

Post 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!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Delay while running after around 20 seconds

Post 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.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Delay while running after around 20 seconds

Post 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.
Help us help you: attach a .love.
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: Delay while running after around 20 seconds

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

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests