12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

474 Part III ■ Advanced Addon Techniquesaddon like the original version <strong>of</strong> Goose that could show or hide frames basedon three different factors (whether or not you were in stealth, in combat, ormounted), it needed eight different states (two possibilities <strong>for</strong> combat timestwo <strong>for</strong> stealth times two <strong>for</strong> mount equals eight) <strong>and</strong> it had to be able toh<strong>and</strong>le all the possible combinations <strong>of</strong> which state to go to when any one <strong>of</strong>these factors changed.Two things make the existing system generally easier to manage. First,snippets are capable <strong>of</strong> examining the game state, attributes on relevantframes, <strong>and</strong> similar factors, directly when triggered to determine the state theyshould set up. Second, the snippets associated with a frame can store as manydistinct pieces <strong>of</strong> in<strong>for</strong>mation as they need to keep track <strong>of</strong>, in private globals.Private Global EnvironmentsEach function has an environment, which is a table where it stores <strong>and</strong> seeksany global variable references made in its body. Because each new functionis created with the same environment as the function that created it, all thefunctions created in the stock UI or addon files normally share the sameenvironment unless they specify otherwise at some point.The functions created when the secure h<strong>and</strong>ler infrastructure compilessnippets use their own environments, which is part <strong>of</strong> how their access togame in<strong>for</strong>mation that’s not supposed to be used <strong>for</strong> secure actions is strictlycontrolled (such as how low on health your target is, <strong>for</strong> example). Becausefunctions are also normally found by calling global variables, functions thatretrieve prohibited in<strong>for</strong>mation simply aren’t available from inside a secureenvironment. But snippets running in that environment can also store theirown globals there, which persist from one call to another. Moreover, all thesnippets associated with a frame typically share an environment, allowingthem to share input into their frame’s state.As a simple example <strong>of</strong> how this can be used, you can fix one <strong>of</strong> the issueswith BlessedMenus, namely, that when you open one menu on a frame <strong>and</strong>then another, the first one stays open behind the second one.Open BlessedMenus.lua <strong>and</strong> look <strong>for</strong> your definition <strong>of</strong> the openSpellListsnippet. Alter it to look like this (changed lines are highlighted):local openSpellList = [[local menu = IsAltKeyDown() <strong>and</strong> “blessings“or IsControlKeyDown() <strong>and</strong> “heals“or IsShiftKeyDown() <strong>and</strong> “h<strong>and</strong>s“menu = menu <strong>and</strong> self:GetFrameRef(menu)if lastMenu <strong>and</strong> lastMenu:IsShown() <strong>and</strong> ilastMenu:GetParent() == self thenlastMenu:Hide()if lastMenu == menu then

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!