why cant i add dt to a variable

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
Zaneishere
Prole
Posts: 3
Joined: Sat Jul 17, 2021 4:30 pm

why cant i add dt to a variable

Post by Zaneishere »

So, i am trying to make a timer but when i tried to add dt to a variable it only gave me a error that says tried to do arithmetic on local dt

Code: Select all

d = 0

function timer(dt)
  d = d + dt
    love.graphics.print(d)
end
the function timer(dt) is in the update function also in the draw function

Code: Select all

function p:update(dt)
  self.y = self.y + self.speed * dt
  timer(dt)
  self:decreas(dt)
  self:releas()
  self:gravity1(dt)
  if m then self:move(dt) end
end
pleas tell me if i did something wrong and how do i fix it
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: why cant i add dt to a variable

Post by GVovkiv »

I guess, that you trying to use dt in love.draw(), since i don't see it in your post
(Maybe full source?)
If you doing something like:

Code: Select all

love.draw = function(dt)
foo = foo + bar * dt
end
It will not work, since love.draw don't give dt as argument, it only happens for love.update(dt), so love throws error about non-number value, which in that case == nil
And if i'm right, just do all your update stuff, well, in update function and never in love.draw
If you absolutly need it, you can use love.timer.getDelta() for that (https://love2d.org/wiki/love.timer.getDelta)
ToastersUnited
Prole
Posts: 5
Joined: Mon Jul 12, 2021 8:22 am

Re: why cant i add dt to a variable

Post by ToastersUnited »

Make sure that when your calling p:update(dt) in main.lua you include (dt) as an argument
Or else its something else
it can only be done in p:update btw
Edit: include timer() inside of p:update amd you can use dt there
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: why cant i add dt to a variable

Post by milon »

Where is p getting called from? Is it getting handed the 'dt' variable correctly?

You could trying this to get an idea of why the error is happening:

Code: Select all

function timer(dt)
  print(d)  -- this will give you console output showing if  d is okay
  print(dt) -- this will give you console output showing if dt is okay
  d = d + dt
    love.graphics.print(d)
end
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Zaneishere
Prole
Posts: 3
Joined: Sat Jul 17, 2021 4:30 pm

Re: why cant i add dt to a variable

Post by Zaneishere »

milon wrote: Wed Jul 21, 2021 1:09 pm Where is p getting called from? Is it getting handed the 'dt' variable correctly?

You could trying this to get an idea of why the error is happening:

Code: Select all

function timer(dt)
  print(d)  -- this will give you console output showing if  d is okay
  print(dt) -- this will give you console output showing if dt is okay
  d = d + dt
    love.graphics.print(d)
end
il try dat also p is a table short for player and ty for the idea
update: i tried your solution and it works ty
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: why cant i add dt to a variable

Post by milon »

Zaneishere wrote: Wed Jul 21, 2021 4:18 pm update: i tried your solution and it works ty
Huh. My "solution" was just for getting more feedback. It shouldn't have changed anything functionally, but I'm glad it works now! :)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Zaneishere
Prole
Posts: 3
Joined: Sat Jul 17, 2021 4:30 pm

Re: why cant i add dt to a variable

Post by Zaneishere »

milon wrote: Thu Jul 22, 2021 1:52 pm
Zaneishere wrote: Wed Jul 21, 2021 4:18 pm update: i tried your solution and it works ty
Huh. My "solution" was just for getting more feedback. It shouldn't have changed anything functionally, but I'm glad it works now! :)
the problem was me printing the variable in the function instead of putting it straight to love.draw
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 20 guests