LUA表字符串到obj

时间:2018-05-28 19:30:40

标签: string object lua key lua-table

是否可以转换对象中的键? 我想简化这段代码:

这有效:

for key, data in pairs(DBButtonOptions[self.value]) do
    if key == "widgetframe1" then
        widgetframe1:SetText(data)
        widgetframe1:SetDisabled(enable)
    elseif key == "widgetframe2" then
        widgetframe2:SetText(data)
        widgetframe2:SetDisabled(enable)
    elseif key == "widgetframe3" then
        widgetframe3:SetText(data)
        widgetframe3:SetDisabled(enable)
    elseif key == "widgetframe4" then
        widgetframe4:SetText(data)
        widgetframe4:SetDisabled(enable)
    elseif key == "widgetframe5" then
        widgetframe5:SetText(data)
        widgetframe5:SetDisabled(enable)
    end
end

工作:

for key, data in pairs(DBButtonOptions[self.value]) do
    key:SetText(data)
    key:SetDisabled(enable)
end

这有效但错误(尝试索引本地密钥):

for key, data in pairs(DBButtonOptions[self.value]) do local key = _G[key] key:SetText(data) key:SetDisabled(enable) end

1 个答案:

答案 0 :(得分:0)

问题似乎像本地变量一样关键。检查此post

我尝试了这个代码段并且运行正常

'meta_value'   => $myId 

只关注内存泄漏

storeFn = _G
storeFn.Gn = {} -- limit iteration of loop "for" and don't get errors
storeFn.Gn.first = function(val) print("test first! "..val) end
storeFn.Gn.second= function(val) print("test second! "..val) end

for k, v in pairs(storeFn.Gn) do
     print( assert (loadstring('return '..'storeFn.Gn.'..k..'(...)')) (math.random()) )
end

也可以使用storeFn.Gn = nil for k,_ in pairs(storeFn) do print(k) end -- restart to original _G 并在销毁后_G.Fn={}