Reputation: 21
Delta time wont work no matter what I try. I get this error
player.lua 39: attempt to perform arithmetic on global 'dt' (a nil value)
in function 'update'
in function 'player_move'
[C] in function: 'xpcall'
I did all the math right, it apparently thinks I'm talking about a non existent variable. Am I supposed to do dt = 1
?
If I do that it still looks like it's moving at different paces occasionally, and no tutorial or documentation I've seen tells you to do that.
Upvotes: 2
Views: 3670
Reputation: 41
You don't have to initialize the dt variable, it is already done for you. If you are trying to get the Delta Time in the love.update
function you do have access to the dt
variable out of the box and wont run into any trouble, but this variable is not global so if you are trying to access this in another place ( Say your love.draw
function or player.lua
) you have three other options:
love.timer.getDelta
function (Recommended)This function is probably the best way to get the delta time. Here is an example usage:
whatever*love.timer.getDelta()
Pretty simple right!
This isn't really the best way to do it, but it is an option. Here is a way to do it and some example usage:
In love.update
:
function love.update(dt)
DeltaTime = dt
end
Some Example Usage:
whatever = whatever*DeltaTime
This isn't a bad way to do it, the real problem with it is that it is that because you are calling your function from love.update
so your function is getting called constantly ( and you might not want this. )
In love.update
:
function love.update(dt)
player_move(dt)
end
Some Example Usage:
function player_move(dt)
whatever = whatever*dt
end
Upvotes: 2
Reputation: 26774
Make sure you define love.update
function as love.update(dt)
(see the example on love wiki); if you call your player_move
function from love.update
, then you'll need to add dt
as a parameter to player_move
and pass the value to it:
local function player_move(dt)
-- do something with dt
end
function love.update(dt)
player_move(dt)
end
Upvotes: 0