Skip to content

Instantly share code, notes, and snippets.

Last active November 28, 2022 22:43
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Leandros/98624b9b9d9d26df18c4 to your computer and use it in GitHub Desktop.
Love2D Fixed Timestep
local TICKRATE = 1/60
function love.update(dt)
function love.draw(dt)
if love.math then
if love.load then love.load(arg) end
local previous = love.timer.getTime()
local lag = 0.0
while true do
local current = love.timer.getTime()
local elapsed = current - previous
previous = current
lag = lag + elapsed
if love.event then
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a
while lag >= TICKRATE do
if love.update then love.update(TICKRATE) end
lag = lag - TICKRATE
if and then
if love.draw then love.draw(lag / TICKRATE) end
Copy link

Great job Leandros!

I also stumbled across:
And your post:

The goal being trying to get constant game tick rate, and variable frame rate (such as users with vsync, or disabling, but having same game experience, or custom FPS cap).

I noticed that your current code differs from 11.X sample here:

I noticed this because your code doesn't work with: Fixed by using love.timer.step()

I took the liberty of converting your version to 11.X:

The updated code is available here:

Also there was some nice discussion about it here: where I added a "spiral of death" fix or otherwise mentioned near the end of the Fix your Timestep article (if simulation takes longer than tick rate)

Last issue was Licensing, it'd be nice if you could provide a license for this. For my updated version, assuming I consider it a forked gist, would be MIT 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment