If an addon has A.lua(at the top of the toc file), B.lua
A.lua:
Code:
local addonName, addon = ...
if not getmetatable(addon) then
setmetatable(addon, {
__index = function(self, key)
-- keep vars in the _G to the addon to reduce the access cost
local v = _G[key]
if v ~= nil then
rawset(self, key, v)
return rawget(self, key)
end
end,
__metatable = true,
})
end
setfenv(1, addon)
-- Standalone code environment, also all files in one addon share the same environment
function myFunc() end
print(_G.myFunc) -- nil, you get print from _G, _G won't get myFunc from your addon
B.lua
Code:
local addonName, addon = ...
setfenv(1, addon)
myFunc() -- you can call the function myFunc defined in A.lua