Search code examples
lualua-tablemetatable

Retrieve an attribute from the same table in LUA


I would like to know if it was possible to retrieve the attribute of a table in the same array. For example here I want to retrieve the "list.index" attribute of my array, how to do?

{
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
},

Solution

  • It's not possible to use an entry in another entry when the table is being created.

    But since you're defining a function, you can do this:

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

    Just make sure you call onSelected with the table as an argument.