1

I am confused as to how to use the timer.delay function. I know the first parameter is the number of seconds of delay; the second is whether it should be repeated; the third is a callback function used after the timer has run down. By my understanding a callback is another function as a parameter. The page on the defold website, though, gives a function with arguments already inserted

      callback function(self, handle, time_elapsed) timer callback function

I am trying to use the timer.delay function like

timer.delay(5, false, pr(self, "handle", "2"))

with

function pr()
    print("Function activated")
BSMP
  • 4,596
  • 8
  • 33
  • 44
  • Hi @Trying_to_work! I'm one of the developers of Defold, and I'm a bit curious as to why you prefer this forum over our own forum dedicated to Defold users? (https://forum.defold.com/) We also have a slack channel where you can ask things too. P.s. also don't forget to mark your questions as solved (if they are), so that those answering your questions, can get their recognition. – JCash May 18 '20 at 06:16

1 Answers1

3

The callback parameter is a function that will be called. When this function is called when your timer triggers. It will be called with 3 arguments which you can use in your callback function, if needed.

the function(self, handle, time_elapsed) in the documentation tells you how your callback function will be called. The first argument is self, the second is the timer handle and the third is the elapsed time.

You could do someting like this:

local function myCallback(obj, handle, elapsed)
  print("Timer with handle " .. handle .. " triggered after " .. elapsed .. "s")
end

timer.delay(10, true, myCallback)

or simply provide an anonymous function:

timer.delay(10, true, function (obj, handle, elapsed)
    print("Timer with handle " .. handle .. " triggered after " .. elapsed .. "s")
  end)
Piglet
  • 27,501
  • 3
  • 20
  • 43