Page 1 of 1

Arithmetic on local 'dt' Error

Posted: Tue Jul 08, 2014 11:13 pm
by aerocom
Heya everyone, I'm new here (and kinda new to coding(I've done some stuff before, I understand a little)), and I have a problem with dt. I want to use it to create smooth movements with my character, but i get the error of "attempt to perform arithmetic on local 'dt' (a nil value)". From some videos I've been watching, I was told that dt is a recognized thing.

Here's the code and where the error is:

Code: Select all

player = {}

function player.load()
	player.x = 5
	player.y = 5
	player.xvel = 0
	player.yvel = 0
	player.friction = 7
	player.speed = 2250
	player.width = 50
	player.height = 50
end

function player.draw()
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
end
--Here is where it says that there's an error:
function player.physics(dt)
	player.x = player.x + player.xvel * dt
	player.y = player.y + player.yvel * dt
	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
end

function player.move(dt)
	if love.keyboard.isDown("right") and
	player.xvel < player.speed then
		player.xvel = player.xvel + player.speed * dt
	end
	if love.keyboard.isDown("left") and
	player.xvel < -player.speed then
		player.xvel = player.xvel - player.speed * dt
	end
end

--More functions including other functions.

function UPDATE_PLAYER(dt)
	player.physics(dt)
	player.move(dt)
end

function DRAW_PLAYER()
	player.draw()
end

If you can help, thanks so much :awesome:

Re: Arithmetic on local 'dt' Error

Posted: Tue Jul 08, 2014 11:19 pm
by Robin
Please next time upload a .love of your project, as per forum rules, that way we are able to better help you.

In this case, it looks like you called UPDATE_PLAYER() instead of UPDATE_PLAYER(dt), so you probably need to change that in your love.update.

Re: Arithmetic on local 'dt' Error

Posted: Tue Jul 08, 2014 11:29 pm
by aerocom
It worked, thank you so much.
And yeah, sorry bout not uploading the .love file. Will do next time.