Thread Tools Display Modes
05-31-14, 07:27 PM   #1
Uitat
A Chromatic Dragonspawn
 
Uitat's Avatar
AddOn Author - Click to view addons
Join Date: May 2011
Posts: 162
trying to understand modules through namespace

what does this do exactly

local addon, ns = ...

local config = ns.config
local bars = ns.bars


my guess is
local addon, ns = ...
is telling a namespace to be made, and the files name is the namespace

and these

local config = ns.config
local bars = ns.bars

are telling the addon that i am registering the other files to be referenced by the current file
  Reply With Quote
05-31-14, 08:57 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,931
local addon, ns = ...

"addon" will contain the name of the addon as per the folder name (a string) and "ns" is assigned a "local" table used to store functions, variables, objects etc. that will be passed to all (.toc rules) associated .lua files (modules) via their own ... parameters. It's a mechanism to avoid having to declare globals in order to access code and data across modules within an addon.

Basic concept:

Addon1.lua
Code:
local addon, ns = ... -- get the addon name and common table.
ns.SomeVar = 666 -- create an entry in the table.
function ns:SomeFunc(arg1) -- create a function in the table.
    print(arg1)
end

-- ... all the other code in the file adding/using "ns" entries also used by other "modules".
Addon2.lua
Code:
local addon, ns = ... -- get the addon name and common table.
ns:SomeFunc(ns.SomeVar) -- prints 666

-- ... all the other code in the file adding/using "ns" entries also used by other "modules".
"addon" and "ns" can be diffent names in each .lua file if that's the way you think, the magic is in the ... being two parameters, a string and a (Blizzard created "local") table.

Addon3.lua
Code:
local myname, mytable = ... -- get the addon name and common table.
mytable:SomeFunc(mytable.SomeVar) -- still prints 666

-- ... all the other code in the file adding/using "mytable" entries also used by other "modules".
I'm sure someone will explain it much more elegantly.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-01-14 at 03:17 AM.
  Reply With Quote
06-01-14, 10:43 AM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Donít message me about addon bugs or programming questions.
  Reply With Quote
06-01-14, 04:13 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,931
"That's all accurate, but somewhat missing the point, I think."

I got the point, you just explained it more betterer .

The magic is in understanding what the ... represents.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
06-01-14, 11:26 PM   #5
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
I believe the technical term in Lua is vararg. Google search can guide you the rest of the way.
Thought I would mention it since it was "omitted".
  Reply With Quote
06-02-14, 04:44 AM   #6
Uitat
A Chromatic Dragonspawn
 
Uitat's Avatar
AddOn Author - Click to view addons
Join Date: May 2011
Posts: 162
thanks to all

ok i do believe i am understanding WAY better now, i appreciate the discussion, as always your all Great, keep up your awsomeness
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » trying to understand modules through namespace

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off