Questions tagged [metatable]

Typically it refers to a feature of Lua which allows a programmer to change the behaviour of tables. The meaning may be dependant on other tags used with it.

In Lua a metatable is used to contain metadata or metafunctions for any Lua value.

There are certain fields within a metatable which (if they exist) have specific responsibilities when used with operators. These are __add, __sub, __mul, __div, __mod, __pow, __unm, __concat, __len, __eq, __lt, __le, __index, __newindex, and __call.

A metatable can have its own metatable and this is known as metatable chaining.

For more information see the Lua reference manual. For a detailed description about meta-tables and meta-methods, see PiL (Programming in Lua). A few of metamethods available are:

  1. Arithmetic Metamethods
  2. Relational Metamethods
  3. Library-Defined Metamethods
  4. Table-Access Metamethods

    • The __index Metamethod
    • The __newindex Metamethod
    • Tables with Default Values
    • Tracking accesses
181 questions
3
votes
1 answer

lua interpreter, having to import file twice for full functionality

so i have a lua file analogous to this: x = { __index = x} constructor = function() local o = {} return setmetatable(o,x) end function x:print() print("hello world") end i type the following into the interpretr: dofile "file.lua" a =…
phat pat
  • 31
  • 1
3
votes
1 answer

Using 'Object' functions in Lua?

I want to be able to have 'objects' with certain functions that refer to themselves (I have no idea what to call this) in Lua. I have seen code of what I'm trying to do but I have never understood what any of it actually means. I have tried looking…
8bitslime
  • 164
  • 1
  • 13
3
votes
1 answer

Lua class not working

I have a simple class implementation in Lua. test = {} test.__index = test function test:new() local o = {} setmetatable(o, self) return o end function test:setName(name) self.name = name print name end local name =…
3
votes
2 answers

Changing metatable in Lua breaks colon operator

While learning Lua, I borrowed some code from here to use string indexing, which is exactly this: getmetatable("").__index = function(str, i) return string.sub(str, i, i) end After that, I wrote a function to reverse a string as practice. function…
charmlessCoin
  • 754
  • 3
  • 13
3
votes
1 answer

lua metatable __lt __le __eq forced boolean conversion of return value

Overloading __eq, __lt, and __le in a metatable always converts the returning value to a boolean. Is there a way to access the actual return value? This would be used in the following little lua script to create an expression tree for an…
chris g.
  • 162
  • 1
  • 4
2
votes
2 answers

Lua metatable class destructor

I have the following Lua metatable class, how can I add a destructor to it so when a certain condition arrives it will destruct the created object and set it's value to nil? ------------------------------------------------- -- Arrow…
Andrei
  • 4,289
  • 4
  • 25
  • 23
2
votes
1 answer

__add not working in custom Vector3 class

I am making a custom Vector3 class for a 3D engine I am making. I am kinda new to lua and not at the stage I should be making a 3D engine, but who cares. My Vector3 script looks like this. #Vector3.lua function Vector3(x,y,z) end Vector3 = { …
2
votes
1 answer

Why doesn't the __call metamethod work in this Lua class?

in the code below, i set up a __call metafunction which, in theory, should allow me to call the table as a function and invoke the constructor, instead of using test.new() test = {} function test:new() self = {} setmetatable(self, self) ---…
2
votes
2 answers

Set the meta table for the lua meta table

The __index of the table originally set a meta table, and the actual access is to the function under this meta table. setmetatable(flatTbl, {__index = metaTbl} I want to access the function of the same name of the meta table when the field of the…
Rider
  • 91
  • 4
2
votes
1 answer

Relying on table comments with MySQL

I'm building an application that adds/removes/alters tables in a MySQL database. Some tables have specific application roles and need to be tagged and identified as such by the application. Can I depend on table comments to reliably store this meta…
leo
  • 2,022
  • 2
  • 17
  • 18
2
votes
2 answers

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 =…
BADZY
  • 51
  • 6
2
votes
1 answer

Lua: Is there a way to get a tables metatable

Is there a way to get a tables metatable? e.g. local tbl = {} setmetatable(tbl, {__call = function() print("tbl called") end}) -- how to get metatable of tbl?
Birk
  • 191
  • 2
  • 12
2
votes
1 answer

Editing multidimensional table with uncertain dimensions in Lua

I want to be able to access and edit values in a user-generated table, that can have any number of dimensions. Say, for this nested table, table = { '1', { '2.1', '2.2' }, { { '3.1.1', …
molevision
  • 23
  • 4
2
votes
1 answer

How to log nested table accesses/writes?

As part of a project I was working on, I wanted to be able to print out whenever a table is accessed or written to, and what was accessed/written to in the table. Looking this up, I found this, which described how to track whenever a table is…
hexy
  • 23
  • 2
2
votes
1 answer

Why doesn't love2d use my custom-defined pairs metamethod?

I have the following code in the file main.lua: local table = {data={a=1,b=2,c=3}} setmetatable(table, table) function table:__pairs() return pairs(self.data) end function table:__tostring() return "a table" end print(table) for e in…
Josie Thompson
  • 5,608
  • 1
  • 13
  • 24