Using the AddOn namespace
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/polluting variables to the global environment.
addonName, addonTable = ...
Vararg
- addonName
- string - The name of your addon as set in the TOC 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
Test.toc
## Interface: 90005 ## Version: 1.0 ## Title: Test FileA.lua FileB.lua
FileA.lua
local _, addonTbl = ... addonTbl.foo = "Hello"
FileB.lua
local addonName, addonTbl = ... print(addonName, addonTbl.foo) -- prints "Test" and "Hello"
Notes
You can also use a unique global variable across your files.
MyCoolAddon = {}
MyCoolAddon.value = 0
function MyCoolAddon:DoSomething()
self.value = self.value + 1
end
References
- Phanx 2014-06-01. WoWInterface - trying to understand modules through namespace. Retrieved on 2019-04-22.
- Gello 2016-02-07. UI and Macro - XML: Loading, Namespaces, Globals and Tips. Retrieved on 2019-04-22.
- Bryce Fischer 2011-11-26. Stackoverflow - Lua, WOW and “…”. Retrieved on 2019-04-22.
- Lua Reference Manual: loadfile, vararg