Change dt or use another timeformat?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- TheEmperorOfLulz
- Prole
- Posts: 7
- Joined: Wed Oct 05, 2011 9:10 am
Change dt or use another timeformat?
Hello all LÖVE users,
I recently started with LÖVE and i want to create a program that counts. However, i need it to not count in dt, but for every second instead. Is there a way i can change the duration of dt? Or do i use another timeformat in this case?
Thanks:)
TehEmperorOfLulz
I recently started with LÖVE and i want to create a program that counts. However, i need it to not count in dt, but for every second instead. Is there a way i can change the duration of dt? Or do i use another timeformat in this case?
Thanks:)
TehEmperorOfLulz
There are 10 types of people. Those that understand binary, and those who don't!
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Change dt or use another timeformat?
Hi there, welcome!
There are several ways to do this.
One option is manually storing "how much time has passed" since the last dt, and trigger the proper action when a second has passed.
Another option is using my library, cron.lua, which basically does the "manual accumulation in a variable" for you.
For example, let's say we have a troll whose energy increases 1 point per second:
If you need things to happen every second, then probably 1 clock is enough for you. But you can create as many clocks as you need. The first parameter of cron.every controls the amount of seconds. You can create an action that happens every 60 seconds, or every 0.5 seconds, if you need to.
In addition to periodic actions, cron also allows to program actions that happens some time in the future, but only once. You can do that with cron.after .
There are several ways to do this.
One option is manually storing "how much time has passed" since the last dt, and trigger the proper action when a second has passed.
Another option is using my library, cron.lua, which basically does the "manual accumulation in a variable" for you.
For example, let's say we have a troll whose energy increases 1 point per second:
Code: Select all
cron = require 'cron'
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
...
trollClock = cron.every(1, function()
troll.energy = troll.energy + 1
end)
...
cron.cancel(trollClock) -- this stops the clock, for example when the troll dies
end
In addition to periodic actions, cron also allows to program actions that happens some time in the future, but only once. You can do that with cron.after .
When I write def I mean function.
Re: Change dt or use another timeformat?
dt is in seconds. It's just a very short amount of time between each update.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Change dt or use another timeformat?
This may be useful to you:
Code: Select all
function love.update(dt)
local wholeSeconds = math.floor(love.timer.getTime())
end
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Change dt or use another timeformat?
I usually do something like this
Code: Select all
function love.load()
time = 0
end
function love.update(dt)
time = time + dt
if time > 1 then
--do something
time = 0
end
end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Change dt or use another timeformat?
You'll lose a few milliseconds with that approach. time could be 1.009 and you set it back to 0. To stay accurate subtract your limit from time.T-Bone wrote:I usually do something like this
Code: Select all
function love.load() time = 0 end function love.update(dt) time = time + dt if time > 1 then --do something time = 0 end end
Shallow indentations.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Change dt or use another timeformat?
Like Boolsheet said, that can be inaccurate. Suppose your app is struggling and running just barely faster than 3 frames per second. The following code will keep you from losing ~ one third of a second every second.T-Bone wrote:I usually do something like this
Code: Select all
function love.load() time = 0 end function love.update(dt) time = time + dt if time > 1 then --do something time = 0 end end
Code: Select all
function love.update(dt)
time = time + dt
if time > 1 then
time = time - 1
end
end
Kurosuke needs beta testers
- TheEmperorOfLulz
- Prole
- Posts: 7
- Joined: Wed Oct 05, 2011 9:10 am
Re: Change dt or use another timeformat?
That all looks awesome. There's just one problem... How do i install it? I really don't know how to do that:)kikito wrote:Code: Select all
cron = require 'cron' function love.update(dt) cron.update(dt) -- you must do this once inside love.update ... trollClock = cron.every(1, function() troll.energy = troll.energy + 1 end) ... cron.cancel(trollClock) -- this stops the clock, for example when the troll dies end
Also, in the code, you say
Code: Select all
trollClock = cron.every(1, function()
TheEmperorOfLulz
There are 10 types of people. Those that understand binary, and those who don't!
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Change dt or use another timeformat?
Just download this file and put it next to your main.lua file (same dir): https://raw.github.com/kikito/cron.lua/master/cron.luaTheEmperorOfLulz wrote: There's just one problem... How do i install it? I really don't know how to do that:)
Then you can include it in main by doing this:
Code: Select all
cron = require 'cron'
It's on purpose. Ignoring the troll.health line, the code is like this:TheEmperorOfLulz wrote: Also, in the code, you sayTo me, it looks like you let out a parenthesis. Is that an error, or did you do it on purpose?Code: Select all
trollClock = cron.every(1, function()
Code: Select all
trollClock = cron.every(1, function()
...
end)
If you have difficulties figuring it out, this code is equivalent to the one on my first post:
Code: Select all
cron = require 'cron'
function increaseTrollEnergy()
troll.energy = troll.energy + 1
end
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
...
trollClock = cron.every(1, increaseTrollEnergy)
...
cron.cancel(trollClock) -- this stops the clock, for example when the troll dies
end
Well, this is not entirely true; the code is equivalent only if the troll variable is available in both increaseTrollEnergy and love.update. If it was a local variable created inside love.update, then you would have to pass it as a parameter - like this:
Code: Select all
cron = require 'cron'
function increaseTrollEnergy(troll)
troll.energy = troll.energy + 1
end
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
...
local troll = something() -- troll is a local variable created inside love.update
...
trollClock = cron.every(1, increaseTrollEnergy, troll) -- we pass it as a parameter
...
cron.cancel(trollClock) -- this stops the clock, for example when the troll dies
end
When I write def I mean function.
- TheEmperorOfLulz
- Prole
- Posts: 7
- Joined: Wed Oct 05, 2011 9:10 am
Re: Change dt or use another timeformat?
Still doesn't work:(
Heres my code:
I added the print function, just to see if it works. But it only works for one second, and then it start counting with dt again, going crazy fast! Where's the error?
Heres my code:
Code: Select all
function love.load()
a = 1
end
cron = require 'cron'
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
trollClock = cron.every(1, function()
a = a + 1
end)
function love.draw()
love.graphics.print(a, 200, 200)
end
end
Last edited by TheEmperorOfLulz on Fri Oct 07, 2011 12:10 pm, edited 1 time in total.
There are 10 types of people. Those that understand binary, and those who don't!
Who is online
Users browsing this forum: No registered users and 10 guests