从 Lua 表中提取数据,WoW 插件

所以我一直在研究 Lua 表格的信息,但我有些困惑。

local profiles = {
{ text = "Profile 1", name = "Profile 1", func = function() 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");

可以看到,我卡在了如何将信息加载进来。当用户点击其中一个菜单项时,会触发该菜单项的函数,例如此处的 loadProfile 函数。

接下来,我认为我需要循环遍历表格,查找名称,一旦找到,就加载所有的变量,但我不确定应该如何实现,或者是否以表格的最佳方式来构造它。

最后,我很难理解下拉菜单的文档,特别是函数调用方面,不知道我的实现是否正确?(示例在这里:http://www.wowwiki.com/API_EasyMenu

非常感谢你们提前的帮助!

点赞
用户1560821
用户1560821

在“业务逻辑”中混合 GUI 不够优雅。

首先,将配置文件单独放在一个表格中:

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

然后,这样书写菜单项:

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, ...)

(您可以很容易地通过程序建立 menuItems,而非手工创建,正如我在这里做的那样。)

我不熟悉《魔兽世界》。我从您提供的网站中获取信息:该菜单回调[具有签名(self, arg1, arg2, checked)](http://www.wowwiki.com/UI_Object_UIDropDownMenu#The_info_table)。

2014-12-24 22:24:13