That's all accurate, but somewhat missing the point, I think.
There's nothing magical about the "local addon, ns = ..." line, and it's not issuing any special instructions, and Lua doesn't have any specific concept of "namespaces". It's just a simple variable assignment.
Let's say your addon has 3 Lua files. Let's call those files File A and File B, and let's say they're loaded in that order. When WoW loads a Lua file, what basically happens is that the contents of the file become the contents of a function, and that function gets run immediately. If your File A says this:
Code:
local name = UnitName("player")
print("Hello " .. name .. "!")
... then WoW basically does this:
Code:
function FileA()
local name = UnitName("player")
print("Hello " .. name .. "!")
end
FileA()
This isn't exactly what happens, but it's an easy way to frame it so that the idea of passing values into the file makes sense:
Code:
local AddonTable = {}
function FileA(...)
local name = UnitName("player")
print("Hello " .. name .. "!")
end
FileA("MyAddon", AddonTable)
function FileB(...)
-- some stuff here
end
FileB("MyAddon", AddonTable)
So now you can see that the code in your question is actually just a simple matter of naming variables, and using variables that refer to strings and tables:
Code:
local AddonTable = {}
function FileA(...)
local addonName, addonTable = ...
local name = UnitName("player")
addonTable.name = name
end
FileA("MyAddon", AddonTable)
function FileB(...)
local addonName, addonTable = ...
print("Hello " .. addonTable.name .. "!")
end
FileB("MyAddon", AddonTable)
No matter how many files are in your addon, they're all passed the
same two values -- a string naming your addon, and a reference to a table. The table doesn't contain any values by default, but it's just a table, so you can put whatever you want in it, and since all of your files are getting a reference to the
same table, anything you add to the table from one file is accessible from the other files.
As Fizzlemizz mentioned, the primary purpose of this table is to let your addon's files share things without having to clutter up the global namespace. However, there's nothing stopping you from putting the table into the global namespace if you really want to:
Code:
local addonName, addonTable = ...
SomeGlobalNameForMyAddon = addonTable