Gideros how to pass a function to Timer.delayCall?

157 Views Asked by At

First, it is possible to do like this:

local delay = 1000
local timer = Timer.delayedCall(delay, 
                                function(data) 
                                    print(data)
                                end, 
                                20)

But, it seems not possible to do like this:

function Game:DoSomething(data)
   print(data)
end

local timer = Timer.delayedCall(delay, 
                                self.DoSomething, 
                                20) 

In other words, I would like to define the function outside (so to be reused by others). However, that seems not possible. Or did I do it wrong?

1

There are 1 best solutions below

0
Nick Elcrome On

If what you want is have Timer.delayedCall call a method with multiple arguments in a generic way, you can do it like this:

function Game:DoSomething(data)
   print(data)
end

function invokeMethod(args)
    local func=table.remove(args,1)
    func(unpack(args))
end

local timer = Timer.delayedCall(
      delay,
      invokeMethod,
      {self.DoSomething,self,20}) --self is the instance you are calling

PS: this is my first post on SO, sorry if it isn't formatted correctly...