this question comes up every now and then.
the problem when creating local functions inside functions is to get the scope right. In your case you have to call the “outside” function first to adress the “inside” function.
I hope you can make sense of this:
local foo = function(params)
local bar1 = function()
print("bar1!")
end
function bar2()
print("bar2!")
end
bar1() -- is local, only works here!
end
foo() -- must be called to declare bar2()...
bar2() -- ...to make this call work
a better approach would be to store your extra functions inside a table:
local myFuncTable = {}
myFuncTable.myFirstFunction = function (param)
print ("WOOOT! It's a "..param )
end
myFuncTable.myFirstFunction ("BOY!")
and the best approach is to ‘outsource’ this table into a module.
func.lua
local myFuncTable = {}
myFuncTable.myFirstFunction = function (param)
print ("WOOOT! It's a "..param )
end
return myFuncTable -- returns the table with all the functions in it
and require it in
main.lua
local myFuncs = require ("func")
myFuncs.myFirstFunction ("BOY!") -- output: WOOOT! It's a BOY!
hope that helps,
happy holidays!
-finefin [import]uid: 70635 topic_id: 19575 reply_id: 75607[/import]