View Single Post
03-07-13, 05:16 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The first line in Myrroddin's example should be:

Code:
local MYADDON, MyAddOn = ...
Every addon gets two variables passed into every one of its Lua files. The first is a string containing the addon's folder/TOC name. The second is a table that's local to the addon, so it's accessible only by the addon's files (unless the addon explicitly adds it to the global namespace) and is handy for sharing stuff between your addon's files that don't need to be shared with the rest of the UI.

However, in Myrroddin's example, the first line isn't actually needed at all, and you can just replace all other instances of "MyAddOn" with "eventFrame" -- if you already have a frame (ie. to listen for events) you don't need to define your functions/methods on another table, you can just define them on the frame.

----------

For bindings, if you are using conditional bindings, you use SetOverrideBinding, not SetBinding. Using SetBinding will permanently overwrite the user's normal bindings; using SetOverrideBinding does nto affect them.

Code:
function MyAddOn:SetBindings()
    -- Make sure you're not in combat:
    if InCombatLockdown() then return end

    -- Temporarily bind "K" to the "Assist Leader" macro:
    SetOverrideBinding(self, nil, "K", "MACRO Assist Leader")
end

function MyAddOn:ClearBindings()
    -- Make sure you're not in combat:
    if InCombatLockdown() then return end

    -- (a) Remove the temporary binding for "K" set by this addon:
    SetOverrideBinding(self, nil, "K", nil)

    -- (b) or, remove *all* temporary bindings set by this addon:
    ClearOverrideBindings(self)

    -- Do either (a) or (b), not both!
end
-----

To bind a key to a macro using Bindings.xml, do this:

Code:
<Bindings>
    <Binding name="MACRO Assist Leader" header="MYADDON">
        -- Nothing goes here.
    </Binding>
</Bindings>
Then define your strings like so:

Code:
BINDING_HEADER_MYADDON = "My AddOn"
_G["BINDING_NAME_MACRO Assist Leader"] = "Assist Leader macro"
Global variables (such as the binding name key) can contain spaces; you just need to quote them as strings and explicitly look them up in the _G table instead of looking them up directly.

----------

Edit:

Aason also has a point. You do need to save permanent bindings. However, you don't need to save override bindings, as they don't actually get "saved" like permanent bindings.

When calling SaveBindings(), always use GetCurrentBindingSet() to get the right "index" value so you don't screw with the user's preference to use account-wide or per-character bindings:

Code:
SaveBindings(GetCurrentBindingSet())
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 03-07-13 at 05:20 PM.
  Reply With Quote