2

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

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 的值而不是变量的方法?

4

1 回答 1

3

使用括号语法。tbl[var]

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

你需要这样做:

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

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

于 2020-11-23T14:28:36.933 回答