Arithmetic on local 'dt' Error

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
User avatar
aerocom
Prole
Posts: 7
Joined: Tue Jul 08, 2014 10:33 pm
Location: Detroit
Contact:

Arithmetic on local 'dt' Error

Post 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:
R u a doge?
Bc im a doge
we cen b doge frends
hurray fer doge frends
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Arithmetic on local 'dt' Error

Post 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.
Help us help you: attach a .love.
User avatar
aerocom
Prole
Posts: 7
Joined: Tue Jul 08, 2014 10:33 pm
Location: Detroit
Contact:

Re: Arithmetic on local 'dt' Error

Post by aerocom »

It worked, thank you so much.
And yeah, sorry bout not uploading the .love file. Will do next time.
R u a doge?
Bc im a doge
we cen b doge frends
hurray fer doge frends
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests