【问题标题】:lua how to get table within table use variable value rather than variable as an index? TTSlua如何在表中获取表使用变量值而不是变量作为索引? TTS
【发布时间】:2020-11-23 14:13:06
【问题描述】:

新手程序员 我正在使用表中的表来使用脚本在桌面模拟器上存储信息

rpgPlayer = 123456 -- this is a string of values
masterTable.rpgPlayer = { test = "test1"} 
for n in pairs(masterTable) do 
    print(n) --- this comes out as rpgPlayer instead of 123456 
    end  

我使用了一个精简的代码示例,但基本上不是在 masterTable 中创建一个 key = 123456 的表,而是创建一个 key = rpgPlayer 是将程序指向 rpgPlayer 的值而不是变量的方法?

【问题讨论】:

    标签: lua tabletop-simulator


    【解决方案1】:

    使用括号语法。 tbl[var]

    点语法(tbl.var)使用字符串变量作为值,这种语法等价于tbl["var"]

    你需要这样做:

    masterTable[rpgPlayer] = { test = "test1" }
    

    阅读有关表格语法的更多信息:https://www.lua.org/pil/2.5.html

    【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2023-03-10
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2021-06-06
    • 2017-04-04
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多