Limit updates per second?

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
Bigmacbook
Prole
Posts: 8
Joined: Sun Feb 19, 2012 8:01 am

Limit updates per second?

Post 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.
User avatar
Rukiri
Citizen
Posts: 68
Joined: Mon Jun 27, 2011 5:52 am

Re: Limit updates per second?

Post 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
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: Limit updates per second?

Post 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
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Limit updates per second?

Post 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
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Limit updates per second?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 140 guests