【问题标题】:Lua: set value on table name from variableLua:从变量中设置表名的值
【发布时间】:2018-08-25 16:15:21
【问题描述】:

我是 Lua 的新手,但目前正在为 tic-80 项目编写库存屏幕。我一直在尝试创建一个函数来绘制更改变量值的按钮。大量的谷歌搜索和一些实验导致了这一点:

function drawButton(x,y,sprite,target,action). 
    [Drawing button stuff here]
    if md==true and mx<=x+12 and mx>=x and my<=y+12 and my>=y then
        _G[target]=action
    end
end

这适用于变量:

drawButton (12,12,0,"eqf",1)

但是当我尝试更改表中的值时,它什么也没做。

drawButton (12,12,0,"actors.player.eqf",1)

有没有更好的方法也支持表格? 提前致谢!

【问题讨论】:

    标签: lua


    【解决方案1】:

    “演员.player.eqf”

    这不涉及嵌套表中的字段。

    如果您想更改任意表中的字段,您需要同时传递表和更新所需的键。像这样的:

    function drawButton(x,y,sprite,object,target,action)
        -- [Drawing button stuff here]
        if md==true and mx<=x+12 and mx>=x and my<=y+12 and my>=y then
            object[target]=action
        end
    end
    
    drawButton (12,12,0, _G, "eqf",1)
    drawButton (12,12,0, actors.player, "eqf",1)
    

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多