Error attempt to perform arithmetic on local 'dt' (a table value)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Wilson_Jebette
Prole
Posts: 3
Joined: Thu Jul 16, 2020 7:09 am

Error attempt to perform arithmetic on local 'dt' (a table value)

Post by Wilson_Jebette »

Having an issue with 'dt'(deltatime), I am confused of why I can't reference dt in main.lua. I've made sure I put dt in all update functions but I still come out with this error.

error: Error
Player.lua:21: attempt to perform arithmetic on local 'dt' (a table value)
Traceback
Player.lua:21: in function 'update'
main.lua:9: in function 'update'
[C]: in function 'xpcall'

I linked my code in an attachment. Im pretty sure I've formatted everything correctly. If you can help me out with this problem I would be forever in your debt :). Thank you for your time and stay safe
Attachments
Player.lua
(949 Bytes) Downloaded 280 times
main.lua
(150 Bytes) Downloaded 293 times
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Error attempt to perform arithmetic on local 'dt' (a table value)

Post by MrFariator »

The error is a subtle one.

In main.lua, you wrote:

Code: Select all

player:update(dt)
...while in Player.lua you wrote:

Code: Select all

function player.update(dt)
  -- rest of the code
end
The difference between a colon (":") and period (".") is important. Consider the following:

Code: Select all

-- method 1
function player.update ( self, dt )
  -- player table is accessible with the passed parameter 'self', you could name it anything, but conventionally it is 'self'
end
player.update(player, dt)

-- method 2
function player:update(dt)
  -- player table is accessible with the local variable 'self'
end
player:update(dt)

-- or even a mix of the two
function player:update(dt)
  -- behaves like method 2
end
player.update(player,dt)
Whenever you use the colon syntax, you are automatically passing the table to the function you're calling implicitly under the variable "self". So in your case, you're mix-and-matching the styles, and so you have a situation where the parameter "dt" gets overridden by the player table you're passing with the colon syntax. This is why you're getting the arithmetic error.
Wilson_Jebette
Prole
Posts: 3
Joined: Thu Jul 16, 2020 7:09 am

Re: Error attempt to perform arithmetic on local 'dt' (a table value)

Post by Wilson_Jebette »

Thank you so much :-) it works now
Post Reply

Who is online

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