Creating a timer in Defold using a Lua module is a fairly straight forward process, covered in separate topics here on the forum (here and here), but I’ve never really liked that you have to call timer.update() yourself. Now, with the alpha release of native extensions, there is the possibility to create a timer module using a native extension and thus get automatic calls to a provided update function from the engine itself. I decided to try this and you can check out the result in the defold-timer project on GitHub. The API looks like this:
local id = timer.seconds(1.5, function(self, id) print("I will be called once, unless I'm cancelled") end) local id = timer.repeating(2.5, function(self, id) print("I will be called repeatedly, until I'm cancelled") end) timer.cancel(id) timer.cancel_all()
Note that native extensions aren’t supported on all platforms yet!