Quantcast WoWInterface - View Single Post - separate modules within 1 addon
View Single Post
11-08-12, 03:50 PM   #4
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,616
Originally Posted by Aanson View Post
Lua Code:
  1. local addon, addonName = ...
  3. core[1] = {};
  4. core[2] = {};
  6. local F, V = unpack(select(2, ...));
I'm not really sure where you are going (or trying to go) with that code, and it should be dying with nil value errors because you're attempting to add values to a core table you never defined as a table (or anything else).

Also, the vararg passed to addon Lua files gives the addon's name first, and the private table second, so your order is backwards:

local addonName, addonPrivateTable = ...
Also also, since you already have a reference to the private table, there's no need to spend a select call to get it again:

local F, V = unpack(addonPrivateTable)
Originally Posted by Aanson View Post
how bad would it be for the Chat_Module.lua functions to be read and stored to memory, but never called?
Not bad at all. Even if your chat module is 100 KB of code, who cares? The static memory usage of a WoW addon is absolutely irrelevant on any machine capable of running WoW in the first place. If you are somehow eating up 100 MB then you will probably want to look into why your chat module was taking up that much memory, but otherwise, remember the two rules of program optimization:

First Rule of Program Optimization: Donít do it.
Second Rule of Program Optimization (for experts only!): Donít do it yet.
If you'd prefer to roll your own module system, I'd go with something like this:

Core file loads first:
addon.modules = {}

function addon:RegisterModule(name, object)
     -- Really, this function is not necessary, but it makes it much easier
     -- to add common features to all of your modules, like self:Print or
     -- other common methods. Also, an API feels cleaner than just adding
     -- stuff to your table from other files.

     self.modules[name] = object

function addon:ADDON_LOADED(name)
     if name ~= addonName then return end
     -- By the time this event fires for this addon, all of
     -- this addon's files and saved variables have loaded.

     -- Initialize your saved variables here.

     -- Do core initialization stuff here.

     for moduleName, moduleObject in pairs(self.modules) do
          if self.db.modules[moduleName].enable then
And in each module file, which loads after the core file:
function module:Enable()
          [color="Silver"][i]-- Do stuff here that should happen when the module loads.

addon:RegisterModule("SomeModule", module)
Then, set up your addon's saved variables to include something like this:
addon.db = {
     modules = {
          SomeModule = {
               enable = true, -- This module is enabled.
               someOtherSetting = 9000,
          OtherModule = {
               enable = false, -- This module is NOT enabled.
               foo = "bar",
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addon ē Turn any code into an addon ē More addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please donít PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote