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 » Sun Sep 13, 2020 9:51 pm

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 68 times
main.lua
(150 Bytes) Downloaded 81 times

MrFariator
Party member
Posts: 164
Joined: Wed Oct 05, 2016 11:53 am

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

Post by MrFariator » Sun Sep 13, 2020 11:12 pm

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 » Thu Sep 17, 2020 12:51 am

Thank you so much :-) it works now

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests