Can I not use dt in local calculations? What is a table value?
Posted: Fri Sep 21, 2018 1:31 pm
Hi,
I'm new to coding, and this is my first project, so forgive me if there's something really simple I'm not understanding yet, but I ran into a problem trying to use dt for my character's movement. I created a new file for the character knight, and tried to include all of his movement in his file so I didn't have to write it in main. Here's knight.lua. The problem is in self.move().
It was a crude attempt but I basically tried to just grab all the code for movement that worked in love.update(dt) in main and stick it in a local function knight.move(). I clearly have no idea what I'm doing lolll
Here's my entire main file.
The error message I get upon running it is knight.lua:16: attempt to perform arithmetic on local 'dt' (a table value)
I guess my big questions are as follows:
What exactly is dt? What is its classification? I want to know exactly what I can or cannot do with it/where I can or cannot use it and why.
If I can actually code my character's movement in his file, what did I do wrong? I need to use dt for calculations, so is there another way of doing that?
Sorry for the long post. Thanks for reading if you did.
I'm new to coding, and this is my first project, so forgive me if there's something really simple I'm not understanding yet, but I ran into a problem trying to use dt for my character's movement. I created a new file for the character knight, and tried to include all of his movement in his file so I didn't have to write it in main. Here's knight.lua. The problem is in self.move().
Code: Select all
knight = {}
knight.new = function()
local self = self or {}
self.x = 200
self.y = 350
self.xvel = 0
self.yvel = 0
self.gdecay = 0
self.adecay = 0
self.grav = 80
self.weight = nil
self.onground = true
self.move = function(dt)
self.x = self.x + self.xvel*dt
self.y = self.y + self.yvel*dt
if love.keyboard.isDown("d") then
self.x = self.x + 10
elseif love.keyboard.isDown("a") then
self.x = self.x - 10
end
if self.onground == true then
self.y = 350
self.yvel = 0
if love.keyboard.isDown("l") then
self.yvel = -1600
self.onground = false
end
end
if self.onground == false then
self.yvel = self.yvel + self.grav
end
if self.y > 350 then
self.onground = true
end
end
return self
end
Here's my entire main file.
Code: Select all
require("knight")
function love.load()
knight = knight.new()
end
function love.update(dt)
knight:move(dt)
end
function love.draw()
love.graphics.rectangle("fill", knight.x, knight.y, 40, 50)
love.graphics.rectangle("line", 0, 400, 900, 300)
end
I guess my big questions are as follows:
What exactly is dt? What is its classification? I want to know exactly what I can or cannot do with it/where I can or cannot use it and why.
If I can actually code my character's movement in his file, what did I do wrong? I need to use dt for calculations, so is there another way of doing that?
Sorry for the long post. Thanks for reading if you did.