Saving a Function name in a Variable

How would I save a function name in a variable to use it later?

I am trying to write my own lib with an onComplete where the onComplete variable holds the name of the function and it will be called in another file.

You should save the function, not the function name (which is a string). You will not be able to invoke a string name without loadbuffer/dobuffer which we disable for security reasons. It is better to save the function anyway because functions in Lua can be anonymous.

I am going to amend my response on further thought. If (and only if) your function has a name associated with it (i.e. not anonymous) and that name is accessible to you (e.g. it is a global variable), you may be able to retrieve the reference to the function by the string name.

For example:

foo = function() print('hello') end

Assuming foo is a global variable, global variables are actually just stored in a table called _G.
So you can get back the function via.

local my_func = _G["foo"]
my_func() -- prints hello

But I stand by my original response that it is much better to just save the reference to the function directly instead of the name.

views:1671 update:2011/10/2 9:44:12
corona forums © 2003-2011