lua - 从 LUA 中的同一个表中检索属性

标签 lua lua-table metatable

我想知道是否可以检索同一数组中表的属性。例如这里我想检索我的数组的“list.index”属性,该怎么做?

{
category_title = "Weapon",
description = nil,
type = "list",
list = {items = {"Give", "Remove"}, index = 1},
style = {},
action = {
   onSelected = function()
      if list.index == 1 then
         -- it's 1
      else
         -- it's 2
      end
   end
},

最佳答案

创建表时不可能在另一个条目中使用一个条目。

但是由于您正在定义一个函数,因此您可以这样做:

   onSelected = function(self)
      if self.list.index == 1 then
         -- it's 1
      else
         -- it's 2
      end
   end

只需确保以表格作为参数调用 onSelected 即可。

关于lua - 从 LUA 中的同一个表中检索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71519610/

相关文章:

python - 如何在 Lua 中直接访问具有多个输出的函数的第 n 个输出

file - 使用Lua如何在不同目录下读写?

lua - 从lua中很长的字符串中获取随机模式匹配的最快方法是什么?

oop - 元表在 Lua (5.2) 中为 OOP 服务的目的是什么?

variables - Lua元表变量声明

inheritance - Lua元表__index定位的区别

lua - io.popen() :lines() ignores first non-empty line

go - 将 Go Struct 实例转换为 Lua 表

lua - 为表分配变量

lua - 在lua中显示表格的内容