Page 1 of 1

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

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

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

Posted: Sun Sep 13, 2020 11:12 pm
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.

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

Posted: Thu Sep 17, 2020 12:51 am
by Wilson_Jebette
Thank you so much :-) it works now