Thread Tools Display Modes
03-25-11, 02:10 PM   #1
daylesan
A Fallenroot Satyr
Join Date: Feb 2011
Posts: 22
Desired addon structure for modules

If I wanted to create a module for an addon, and said module controlled whether or not (or how) some of the addon's functions execute, which would be the best approach?

Say I have this in my addon:

LUA Code:
  1. function MyFunction(arg1, arg2)
  2.     if arg1 and arg2 then
  3.           -- do stuff
  4.     end
  5. end


If the module is running, I basically want the whole thing to behave behave as if it had the following code:


LUA Code:
  1. function MyFunction(arg1, arg2)
  2.     if arg1 and arg2 and not someVar then
  3.           -- do stuff
  4.     end
  5. end

Should I use hooks for this? Are hooks viable once there are multiple modules that all want to influence how MyFunction executes?

Is it better to put the conditions from MyFunction into a table to which my modules can add/remove their own code as required?

Or create a dummy someVar that only changes if modified by the module?

What is generally considered to be the best approach?

Last edited by daylesan : 03-25-11 at 03:02 PM.
  Reply With Quote
03-25-11, 05:43 PM   #2
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
Don't use hooks. It will screw up once you allow users to enable/disable modules in game on demand.

Instead, create an table that will contain functions that modules can register and unregister with the main addon. The main addon then iterates through all registered functions in the table and calls each one in turn.

Code:
local funcs = {}
function MyAddon:RegisterModuleFunc(modulename, func)
    funcs[modulename] = func
end

function MyAddon:UnregisterModuleFunc(modulename)
    funcs[modulename] = nil
end

function MyAddon:MyFunction(arg1, arg2)
    for moduleName, moduleFunc in pairs(funcs) do
        -- Do stuff
        moduleFunc(arg1, arg2)
    end
end
Something like that, you can be fancier, have more arguments, have different tables, etc.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes

Last edited by Xinhuan : 03-25-11 at 05:46 PM.
  Reply With Quote
03-26-11, 05:21 AM   #3
daylesan
A Fallenroot Satyr
Join Date: Feb 2011
Posts: 22
Thanks for the excellent answer.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Desired addon structure for modules


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