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

Why short calling form not working for table in Lua 5.3?

Lua 5.3 This doesn't work, why ? Why short calling form not working for tables ? t = { "a", "b", "c" } s = t:concat() this works ... s = table.concat(t)
voidptr
  • 21
  • 2
2
votes
0 answers

Lua setting metatable functions for C++

I found 2 types of code for implementing metatable functions. I don't really understand the first, and what's the difference with the second. The first: lua_newtable(l); int methods = lua_gettop(l); luaL_newmetatable(l, "MapCreator"); int metatable…
neiji93
  • 21
  • 4
2
votes
1 answer

Confusion of using "." notation with __index and namespace in Lua

I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index? …
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
2
votes
1 answer

lua metatable help (generating monsters within game)

I am having trouble using a metatable to create new monsters for a game, I can create an exact copy but I can't generate a new rat or lizard for example with a new id. local monsters = { rat = { name = "a rat", id = 1, health = 5, } …
2
votes
1 answer

time complexity of metatable in Lua when accessing

local cls = {base = "base"} local ins = {} cls.__index = cls setmetatable(ins, cls) What is the time complexity of accesssing ins.base?
1hunch1kill
  • 522
  • 4
  • 12
2
votes
3 answers

Specifying metatables on new objects in Lua

Method/1 Dog = {} function Dog:new() local newObj = {sound = 'woof'} return setmetatable(newObj, { __index = self }) end Method/2 Dog = {} function Dog:new() local newObj = {sound = 'woof'} self.__index = self return…
George
  • 15,241
  • 22
  • 66
  • 83
2
votes
2 answers

Lua - calls only one operator in metatable

I'm new to Lua, so maybe missed something on tutorials but the problem is: I have original table and metatable with several operators that I'm applying to it: original = { 1, 2, 3 } test = setmetatable(original, { __add = function (lhs, rhs) …
Kosmetika
  • 20,774
  • 37
  • 108
  • 172
2
votes
1 answer

Lua C API - mapping a property to a function

Is it possible to push a function as a property in Lua? Right now, I can have Get/Set functions by pushing them as fields like so: lua_pushcfunction(L,L_Getter); lua_setfield(L, -2, "GetValue"); lua_pushcfunction(L,L_Setter); lua_setfield(L,…
Grapes
  • 2,473
  • 3
  • 26
  • 42
2
votes
2 answers

LuaInterface C# Metatables

I have some background lua codes and I load them on each startup, They work fine, But Is there any way I can convert them to C#? Here Is my meta table codes: player = {} player.__index = player function player:Add(name) return…
111WARLOCK111
  • 857
  • 2
  • 7
  • 14
2
votes
3 answers

Class structure in Lua/Corona

So I'm working on a game in Lua and I'm trying to use metatables and classing but I think I'm importing my PHP knowledge and doing things slightly sideways. -- Basic Monster Monster = {} function Monster:new(newX, newY) local newMonster = {x =…
DaOgre
  • 2,080
  • 16
  • 25
2
votes
1 answer

Inheriting from an immutable types?

I just answered a Python question which "required" inheriting a class from int (immutable type in Python). I've been doing some Lua lately and as I answered the question, I thought; "How is this done in Lua?" So, can I inherit a class from an…
user2032433
2
votes
2 answers

How do I prevent my callable Lua table passing itself as an argument to __call?

Say I have the following code: local t = {}; setmetatable(t, {__call=print}); t(3, 5, 7) Instead of printing: 3 5 7 it prints: table: 0x9357020 3 5 7 The id of the table is that of t. How can I make it behave as though I called print…
JasonFruit
  • 7,764
  • 5
  • 46
  • 61
2
votes
1 answer

Catching a call to non existing function in Lua

I'd like to create a simple mock table that would tell me what was tried to be called from it. My first try was: local function capture(table, key) print("call to " .. tostring(table) .. " with key " .. tostring(key)) return…
vertti
  • 7,539
  • 4
  • 51
  • 81
2
votes
1 answer

Does Lua __gc metamethod now work for table (Lua 5.2.1) ?

I've been a little surprised, because I have read before, that __gc metamethod is only called for userdata and never for tables. (LuaFAQ : Why don't the __gc and __len metamethods work on tables?) But, recently, I have tried it and found it actually…
Seagull
  • 13,484
  • 2
  • 33
  • 45
2
votes
1 answer

Lua metatables and metamethod - How to call a different member function

I have the following Class local PROGRESS = {} PROGRESS.__index = function(self,key) if key~="__group" and self.__group[key] then return self.__group[key] else return rawget(self,key) end end What this…
SatheeshJM
  • 3,575
  • 8
  • 37
  • 60