View Single Post
01-30-13, 12:05 PM   #5
Farmbuyer
A Cyclonian
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 43
Originally Posted by A_Nolan View Post
ok the sticky said to enter the entire code so i'v put in the main part. Is PLAYER_DIFFICULTY_CHANGED the correct event to pull when the dungeon difficulty (normal/heroic) is toggled. I have the correct setup to get the difficulty but can't get the thing to fire when i toggle it.
I remember that the only thing Blizzard registers to listen for that event is the minimap, to change the little icon cluster. If you haven't already, extract their code and take a look at FrameXML/Minimap.* to see what they're doing.

Next, some random tips for Lua addons:

Code:
function FIZ_OnEvent(self, event, ...)
    local arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13 = ...
You can also just capture the arguments that you know you'll need later, and then give them meaningful names. Easier to maintain and saves a little memory:

Code:
-- Nothing I'm listening for uses arg3/4/5 or 7+
function FarmForum_OnEvent (self, event, arg1, arg2, _, _, _, arg6)
    ......
    elseif event == "STUFF_WITH_SPELLS" then
        local partymember, spellname = arg2, arg6
        do_Foo (me, him, partymember, the_thing, spellname)

    elseif event == "CHEESE_GOING_BAD" then
        local bagslot, flavor = arg1, arg2
        expiringCheese (bagslot, flavor, stuff)
Eventually, you'll almost certainly want to split out each event handler into its own function, so that your OnEvent doesn't become gigantic and hard to maintain. An event dispatcher like this is quite common:

Code:
function FarmForum_OnEvent (self, event, ...)
    local handler = self[event]
    if type(handler) == "function" then
        -- passing the event name is a little redundant for this example but is useful in the general case
        return handler (self, event, ...)
    end
end
Then your individual handler functions are named after the event, and can also name their arguments directly:

Code:
local MyFrame = Global_Name_of_FarmForum_Frame_From_Wherever_I_Declared_It

function MyFrame:ADDON_LOADED (event, addon_name)
    -- addon_name is arg1 here but is also a more self-explanatory name
end

function MyFrame:PLAYER_ENTERING_WORLD()
    -- doesn't take any args
end

function MyFrame:CHEESE_GOING_BAD (event, bagslot, flavor)
    print("Dammit!  The", flavor, "cheese in slot", bagslot, "is turning green!")
end
  Reply With Quote