zeroflaw
zeroflaw

Reputation: 564

Gideros how to pass a function to Timer.delayCall?

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?

Upvotes: 0

Views: 159

Answers (1)

Nick Elcrome
Nick Elcrome

Reputation: 11

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...

Upvotes: 1

Related Questions