Can I not use dt in local calculations? What is a table value?

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
mejonat
Prole
Posts: 2
Joined: Fri Sep 21, 2018 12:07 pm

Can I not use dt in local calculations? What is a table value?

Post by mejonat »

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().

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
It was a crude attempt :crazy: 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.

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
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.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can I not use dt in local calculations? What is a table value?

Post by pgimeno »

Hello, welcome to the forums.

The problem here is that you're using colon syntax to call knight:move(dt), which is OK, but you're not using it nor adding the implicit 'self' argument when declaring the function. Take into account that this syntax:

Code: Select all

table:method(params)
is equivalent to this:

Code: Select all

table.method(table, params)
It said it was a table, because in place of 'dt' it sent the table. 'dt' was passed as the second argument, which you didn't even declare.


Now, to fix it, you can replace this line:

Code: Select all

  self.move = function(dt)
with this:

Code: Select all

  function self:move(dt)
or even with this:

Code: Select all

  self.move = function(self, dt)
(the last two are equivalent).
mejonat
Prole
Posts: 2
Joined: Fri Sep 21, 2018 12:07 pm

Re: Can I not use dt in local calculations? What is a table value?

Post by mejonat »

Thank you! This fixed my problem.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 48 guests