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.

346 Part III ■ Advanced Addon Techniquesend,[“hide“] = function()print(“Hiding“)end,[“help“] = “GUI comm<strong>and</strong>s: width , height , show, ihide“},[“data“] = {[“load“] = function(pr<strong>of</strong>ile)print(“Loading pr<strong>of</strong>ile:“, pr<strong>of</strong>ile)end,[“save“] = function(pr<strong>of</strong>ile)print(“Saving pr<strong>of</strong>ile: “, pr<strong>of</strong>ile)end,[“reset“] = function()print(“Resetting to default“)end,[“help“] = “Data comm<strong>and</strong>s: load , save , reset“},[“help“] = “Comm<strong>and</strong>Table comm<strong>and</strong>s: gui, data“}As you can see, this is a very concise way <strong>of</strong> defining a wide range <strong>of</strong>functionality. That is, once you get the engine out <strong>of</strong> the way. Begin thecomm<strong>and</strong> processing function with the following code:local function DispatchComm<strong>and</strong>(message, comm<strong>and</strong>Table)local comm<strong>and</strong>, parameters = string.split(“ “, message, 2)local entry = comm<strong>and</strong>Table[comm<strong>and</strong>:lower()]local which = type(entry)The first line <strong>of</strong> the function splits the message at the first space. Notice theextra argument <strong>of</strong> 2, which tells string.split to return a maximum <strong>of</strong> twostrings, limiting the number <strong>of</strong> splits.The next line retrieves the entry from the given comm<strong>and</strong> table, then thefunction determines what type <strong>of</strong> entry it is. The rest <strong>of</strong> the function isessentially a restatement in Lua <strong>of</strong> our earlier description:if which == “function“ thenentry(parameters)elseif which == “table“ thenDispatchComm<strong>and</strong>(parameters or ““, entry)elseif which == “string“ thenprint(entry)elseif message ~= “help“ thenDispatchComm<strong>and</strong>(“help“, comm<strong>and</strong>Table)endend

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

Saved successfully!

Ooh no, something went wrong!