Page 1 of 1
Limit updates per second?
Posted: Fri Aug 22, 2014 1:39 am
by Bigmacbook
I want to limit how many times love.update runs to 20 ticks a second but i don't want it to impact my FPS how would I do that?
This is what Minecraft does, it only updates player data 20 times a second but you get up to unlimited frames.
Re: Limit updates per second?
Posted: Fri Aug 22, 2014 3:01 am
by Rukiri
This is how I've gone about doing it, but why would you want to limit? Slow motion effect?
Code: Select all
if not focus then return end
if dt > 1/30 then
dt = 1/30
end
Re: Limit updates per second?
Posted: Fri Aug 22, 2014 5:25 am
by Wojak
Bigmacbook wrote:
This is what Minecraft does, it only updates player data 20 times a second but you get up to unlimited frames.
Are you sure?
Stuff like position update must be done per frame, else it will not be smooth...
Minecraft may send the player data to the server 20 times per second, but I think that clientside player position is updated per frame, and this includes the remote players – though the position is not send from the server per frame, the client moves the players between the last known two positions by interpolating them and updating the positions per frame.
Also prove that updating position 20 times per secound is not smooth even with high fps:
Code: Select all
function love.load()
x = 200
y = 50
r = 10
speedx = 200
speedy = 200
end
local dt2 = 0
function love.update(dt)
dt2 = dt2 + dt
if dt2 >= 1/20 then
x = x - speedx * dt2
y = y - speedy * dt2
if (x + r >= 800) and speedx < 0 then
left = not left
speedx = 0 - speedx
elseif (x - r <= 0) and speedx > 0 then
left = not left
speedx = 0 - speedx
end
if (y + r >= 600) and speedy < 0 then
down = not down
speedy = 0 - speedy
elseif (y - r <= 0) and speedy > 0 then
down = not down
speedy = 0 - speedy
end
dt2 = 0
end
end
function love.draw()
love.graphics.circle("fill",x,y,r);
end
Re: Limit updates per second?
Posted: Fri Aug 22, 2014 6:56 am
by Plu
If you want to do it the minecraft way, like Wojak says, you can't just limit how often the update function runs, because that would mean that while you have lots of frames, nothing would actually happen in the extra frames, because all of the animations and such run in update, which isn't called.
Instead what you want is to have two update loops, one that only does animation work and one that updates everything else. I'm guessing if you really want this that you'd either:
A) modify love.run to work with 2 distinct update loops, one for animations and tweening and one for heavy update work
B) make a seperate thread that does the heavy updating, and pull data from it into the main update thread at a fixed rate
Re: Limit updates per second?
Posted: Fri Aug 22, 2014 9:17 pm
by DaedalusYoung
Code: Select all
local timer = 0
function love.update(dt)
timer = timer + dt
while timer >= 0.05 do
timer = timer - 0.05
updateRedstone()
end
end
will only run the updateRedstone function 20 times per second, while being able to run at any fps. Of course, with lag spikes or framerates under 20, the redstone function will be called several times per frame, which may not be what you want, but it shouldn't be too hard to fix that.
Then you can have a player function still update every frame, so movement remains smooth.