Basic timer usage

I think I've misunderstood something simple about timers.

I expected this to print to the console once evert 3 seconds, but it doesn't print anything.

import "CoreLibs/graphics"
import "CoreLibs/object"
import "CoreLibs/timer"

local pd <const> = playdate
local gfx <const> = pd.graphics
local geo <const> = pd.geometry
local point <const> = geo.point
local timer <const> = pd.timer

local myTimer <const> = timer.new(3000, timerCallback)
assert(myTimer ~= nil)
myTimer.discardOnCompletion = false
myTimer.repeats = true

function timerCallback()
    print(string.format("Tick @ %d", pd.getCurrentTimeMilliseconds()))
end

function pd.update()
    gfx.sprite.update()
    timer.updateTimers()
end

What am I missing?

I hadn't realised that the function needs to be declared before it can be used, therefore moving the timerCallback() above the line creating the timer solved the problem.

4 Likes