Using the AddOn namespace
Jump to navigation
Jump to search
The addon namespace is a private table shared between Lua files in the same addon.
It allows for addons to access data between files without exposing or polluting variables to the global environment.
addonName, addonTable = ...
Vararg
- addonName
- string - The name of your addon as in the TOC file and folder name.
- addonTable
- table - The shared addon table between the Lua files of an addon.
Example
This addon defines the "foo" key on the addon table in FileA.lua
and accesses it in FileB.lua
MyAddon.toc
## Interface: 100207 ## Version: 1.0.0 ## Title: Test FileA.lua FileB.lua
FileA.lua
- Note that the underscore "_" here is used as a throwaway variable when we don't really care about it.
local _, ns = ...
ns.foo = "Banana"
FileB.lua
local addonName, ns = ...
print(addonName, ns.foo) -- prints "MyAddon" and "Banana"
Notes
You can also simply use a unique global variable.
FileA.lua
MyAddon = {}
MyAddon.value = 0
function MyAddon:DoSomething(increment)
self.value = self.value + increment
end
MyAddon:DoSomething(2)
FileB.lua
MyAddon:DoSomething(3)
print(MyAddon.value) -- 5
Patch changes
- Patch 3.3.0 (2009-12-08): Added.
References
- haste 2010-06-28. WoWInterface: ns ?!.
- Phanx 2014-06-01. WoWInterface - trying to understand modules through namespace. Retrieved on 2019-04-22.