如何在表的键中分配多个值?

Hea*_*tso 3 arrays lua local-variables

我正在尝试为表中的一个变量分配多个值。一个用于字符串名称,另一个用于整数。该代码将去:

items = {
potion = "Potion", 100
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何正式编写此代码,以及如何调用这些特定值。(你这样称呼吗?)

io.write(item.potion.1) --> Potion
io.write(item.potion.2) --> 100
Run Code Online (Sandbox Code Playgroud)

(或者是其他东西?)

请帮忙。:一世

Pau*_*nko 6

您可以将这些值分配给由数字或标识符索引的表:

-- identifiers
items = {
  potion = {name = "Potion", value = 100},
}
print(items.potion.name, items.potion.value)

-- numeric indexes
items = {
  potion = {"Potion", 100},
}
print(items.potion[1], items.potion[2])
Run Code Online (Sandbox Code Playgroud)

我个人更喜欢前一种方法(尽管更冗长一些,但它更易于维护),但是任何一种都应该起作用。