local and global function by external module

Hi guys.
I’ve a problem with external modules. How can i call a local function by external module?

Example.
I have some buttons in an external module and the button function in main.lua . When the function is global it is called, but when it is local the buttons don’t see it . How can i do to solve?

Assuming I’ve understood the question correctly, if you have a local function in a module then it will only be  visible to that module. If you have a local function that you want to expose you could do something like this.

buttons.lua:

local buttons = {} local function myPrivateFunction() -- this is only visible in this module end function buttons.myPublicFunction() -- this will be accessible via the returned buttons table end return buttons

main.lua

local buttons = require("buttons") -- we can now call the function buttons.myPublicFunction()
1 Like

Maybe this post will help clarify. It’s a bit old but still valid.

http://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

No. My question is different.

I’ve something like this

–main lua
local but = require “buttons”
but.button()
local function handlebuttonevent
– function activate by buttons in module
end

–buttons.lua
local buttons = {}
local function buttons.button()
–widget buttons who call local handlebuttonevent in main
end
Return buttons

If i insesrt local in function handlebuttonevent it doesn’t call. If the function is global it is called normally.

buttons.lua will not be able to see the function handlebuttonevent in main.lua because that function is local to main.lua. You could pass it into the button like this:

-- main.lua local function handlebuttonevent() -- function activated by buttons in module end local but = require "buttons" but.button(handlebuttonevent) -- buttons.lua local buttons = {} function buttons.button(handlebuttonevent) -- this can now call handlebuttonevent end

Thanks for help
the local function run :slight_smile:

Assuming I’ve understood the question correctly, if you have a local function in a module then it will only be  visible to that module. If you have a local function that you want to expose you could do something like this.

buttons.lua:

local buttons = {} local function myPrivateFunction() -- this is only visible in this module end function buttons.myPublicFunction() -- this will be accessible via the returned buttons table end return buttons

main.lua

local buttons = require("buttons") -- we can now call the function buttons.myPublicFunction()

Maybe this post will help clarify. It’s a bit old but still valid.

http://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

No. My question is different.

I’ve something like this

–main lua
local but = require “buttons”
but.button()
local function handlebuttonevent
– function activate by buttons in module
end

–buttons.lua
local buttons = {}
local function buttons.button()
–widget buttons who call local handlebuttonevent in main
end
Return buttons

If i insesrt local in function handlebuttonevent it doesn’t call. If the function is global it is called normally.

buttons.lua will not be able to see the function handlebuttonevent in main.lua because that function is local to main.lua. You could pass it into the button like this:

-- main.lua local function handlebuttonevent() -- function activated by buttons in module end local but = require "buttons" but.button(handlebuttonevent) -- buttons.lua local buttons = {} function buttons.button(handlebuttonevent) -- this can now call handlebuttonevent end

Thanks for help
the local function run :slight_smile: