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
2
votes
1 answer

Lua metatable variable declaring

I did not know what to name this question as I don't understand enough of what is going on.(Feel free to edit) Consider the code below. function object:new() o = o or { x = 0 } setmetatable(o, self) self.__index = self …
user805687
1
vote
1 answer

Lua __lt not working with table.sort even though it is defined

I've got a tile class that inherits from an entity class. The entity class defines __lt, and though I can access it from the tile class, I get a "comparing two tables" error when I try to put all tiles through table.sort. Entity Class local Entity =…
1
vote
1 answer

Lua - How to log nested table access/writes without creating a "proxy" index outside of the metatable?

There's a method where you'd be doing something like this to the original table. function setproxy(t) new_t = {proxy = t} setmetatable(new_t, mt) return new_t end However, if you'd be printing out t, you'd get this: t["proxy"]. So I was…
karl-police
  • 983
  • 8
  • 21
1
vote
1 answer

How to use metatables to monitor changes made to an embedded table? (Lua)

I am looking to monitor changes made to one particular embedded table, in this case oranges, using the __newindex metamethod. My current code is able to detect the change, yet the value will obviously not update. I understand that the original data…
1
vote
2 answers

Lua given table A with metatable B, have functions in B defer to properties in A *before* identically-named properties in B

So i'm making a system that runs through a list of "Tutorial Steps" during a tutorial, in sequence. the tutorial steps could be anything, but to guarantee some shared structure i've made an 'abstract' class TutorialStep. each unique type of tutorial…
Swanijam
  • 77
  • 1
  • 8
1
vote
1 answer

Nesting of operators in Lua does not give correct answer

The following code defines sort of custom operator imp. It works almost correctly. local function _imp(x, y) if x == 1 and y == 0 then return 0 else return 1 end end local tmp = {} local imp = {} local…
mathsbeauty
  • 364
  • 4
  • 13
1
vote
1 answer

Currying operations in lua

This question has some reference to the question Defining logical operator implies in lua. The following code works fine. local function _implies(a, b) if a == 1 and b == 0 then return 0 else return 1 end end local…
mathsbeauty
  • 364
  • 4
  • 13
1
vote
2 answers

Return table value index via metatable

I'm looking for a way to retrive index value via metatable. This is my attempt: local mt = { __index = { index = function(t, value) local value = 0 for k, entry in ipairs(t) do if…
user3713179
  • 361
  • 3
  • 12
1
vote
1 answer

(Lua) Simple metatable in another file

I'm sorry that it is such a basic issue but I can't seem to understand. I'm a beginner in Lua and so I have several questions. My goal is to make a small "class" Vector3. I'm using a metatable that I put in a file vector3.lua. 1) Since my metatable…
Meteion
  • 13
  • 2
1
vote
1 answer

Lua Get/Set Metatable

local ents = { GetLocalPlayer = function() local tbl = { localplayer = {"Ava", "1", {213,234,234}}, GetIndex = function(self) return self.localplayer[2] end, } setmetatable(tbl,…
Carter
  • 75
  • 2
  • 9
1
vote
2 answers

Why does setting the value of nested table set the metatable's table?

I'm learning lua and just discovered a quirk to metatables: local mt = { value = 0, nested = { value = 0 }, } mt.__index = mt local t1 = {} setmetatable(t1, mt) local t2 = {} setmetatable(t2, mt) print(t1.value) --…
SHiLLySiT
  • 153
  • 1
  • 13
1
vote
1 answer

Why does an error pop up after trying to append?

table1 = {1,2,3,4,5} table2 = {1,2,3,4} metatable ={__add = function(table,otherTable) sumTable = {} for i=1, #table do table.insert(sumTable,table[i]) end for i=1, #otherTable do table.insert(sumTable,otherTable[i]) …
cembed
  • 13
  • 3
1
vote
1 answer

Lua - C++ object metatable returns nil

I have just started delving into Lua, and I learnt that C++ object properties could be accessible through metatables. I am trying to access such an object's functions in a game's script: "GameLib". It is available in Lua as a global variable, but…
Zsolt DOKA
  • 13
  • 2
1
vote
1 answer

Lua modify metatable properties from parent table

I have this code, but I don't want the tbl variable getting width and height assigned to it, rather I want base_table's width and height properties getting modified to 200 rather than tbl getting assigned a height and width of 200. function…
noobyy
  • 123
  • 9
1
vote
1 answer

Is it possible to dump the properties/methods of a type userdata variable?

Hi I'm wanting to get all the properties and methods of a type function that returns userdata. The documentation on the API i'm using is quite poor, and I know there are more stuff not documented. local w = gui.CreateWindow(...) -- "returns…
noobyy
  • 123
  • 9