Extracting data from Lua table, WoW addon

1.4k views Asked by At

so I've been pouring over information on Lua tables and I'm a little stuck.

local profiles = {
{ text = "Profile 1", name = "Profile 1", func = fuction() loadProfile("Profile 1");, INT = .5, STR = .5, AGI = .5, CRIT = .5, HASTE = .5, MAS = .5, MS = .5, VERS = .5},
{ text = "Profile 2", name = "Profile 2", INT = .6, STR = .6, AGI = .6, CRIT = .6, HASTE = .6, MAS = .6, MS = .6, VERS = .6},
{ text = "Profile 3", name = "Profile 3", INT = .7, STR = .7, AGI = .7, CRIT = .7, HASTE = .7, MAS = .7, MS = .7, VERS = .7},
{ text = "Profile 4", name = "Profile 4", INT = .8, STR = .8, AGI = .8, CRIT = .8, HASTE = .8, MAS = .8, MS = .8, VERS = .8},  
}

function loadProfile(name)
--Loop through table using pairs
  --Once name is found, load INT into INTELLECTSTAT, etc
end

local profilesDropDown = CreateFrame("Frame", nil, ActualValue, "UIDropDownMenuTemplate")
menuFrame:SetPoint("TOPLEFT", ActualValue, 300, -40)
EasyMenu(profiles, profilesDropDown, ActualValue, 300 , -40, "Profiles");

As you can see I'm stuck at loading the info in, when the user clicks on one of these menu items it triggers the function of that menu item, in this case loadProfile.

Next I think I need to loop through the table looking for name, and once found load all the variables, but I'm unsure of how to do that or if I'm even structuring the table in the best way I can.

And finally I'm having a hard time understanding the documentation for the DropDown in terms of the function call and was wondering if I was doing it right? (Example of it being used at bottom http://www.wowwiki.com/API_EasyMenu)

Thanks for all the help in advance guys!

1

There are 1 answers

0
Niccolo M. On BEST ANSWER

It's not elegant to mix the GUI with the "business logic".

First, make the profiles a separate table:

local profiles = {
  ["Profile 1"] = {
    INT = .5, STR = .5, AGI = .5,
  },
  ["Profile 2"] = {
    INT = .6, STR = .6, AGI = .6,
  },
}

Then, write your menu items thus:

local function loadProfile(_, prof)
  INTELLECTSTAT = prof.INT
end

local menuItems = {
  { text = "Profile 1", func = loadProfile, arg1 = profiles["Profile 1"] },
  { text = "Profile 2", func = loadProfile, arg1 = profiles["Profile 2"] },
}
...
EasyMenu(menuItems, ...)

(You can easily build the menuItems programatically instead of doing it by hand as I did here.)

I'm not familiar with WoW. I took the information from the site you linked to: it says that the menu callbacks have the signature (self, arg1, arg2, checked).