2,087
edits
Line 90: | Line 90: | ||
Beware Lua functions and variables are globals by default and are directly visible to the other addons. If authors blindly use simple and common names, like: | Beware Lua functions and variables are globals by default and are directly visible to the other addons. If authors blindly use simple and common names, like: | ||
<pre> | |||
addonEnabled = true; | addonEnabled = true; | ||
function GetArrowsCount() | function GetArrowsCount() | ||
... | ... | ||
end | end | ||
</pre> | |||
An addon will easily conflict with variables of the same name from other addons or from the Blizzard interface. Instead, you should write your addon more like a Lua module: place all the data and functions in a single global table, named after the addon, and use local declarations otherwise (no other globals), like: | |||
<pre> | |||
MarksmanAddOn = { }; -- about the only global name in the addon | MarksmanAddOn = { }; -- about the only global name in the addon | ||
local addonEnabled = true; -- local names are all ok | local addonEnabled = true; -- local names are all ok | ||
Line 101: | Line 105: | ||
end | end | ||
MarksmanAddOn.arrowsCount = MarksmanAddOn:GetArrowsCount() | MarksmanAddOn.arrowsCount = MarksmanAddOn:GetArrowsCount() | ||
</pre> | |||
For large add-ons this can get complicated, and the current version of Lua doesn't offer much to help. Use a full, clear, name for the global table, and assign a shorter name in your .lua files, like: | For large add-ons this can get complicated, and the current version of Lua doesn't offer much to help. Use a full, clear, name for the global table, and assign a shorter name in your .lua files, like: | ||
<pre> | |||
local mod = MarksmanAddOn; | local mod = MarksmanAddOn; | ||
-- then you can write | -- then you can write | ||
mod.arrowsCount = mod:GetArrowsCount() | mod.arrowsCount = mod:GetArrowsCount() | ||
</pre> | |||
Other than the global table for the addon, you may still need globals for the saved variables and the addon slash commands (see below) if you have any. | Other than the global table for the addon, you may still need globals for the saved variables and the addon slash commands (see below) if you have any. | ||