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

Hello metatable.__len world

A beginner's question about Lua and metatables, with a example as simple as an Hello‑World, involving the len event, which unfortunately does not returns the expected result (I'm using Lua 5.1 installed from Ubuntu's official repository). The…
Hibou57
  • 6,870
  • 6
  • 52
  • 56
3
votes
1 answer

Ignoring metatable in Lua?

I'm working on a Lua menu, and I got Menu class which creates instances like this: function Menu:new(label, action, open) local inst = { parent = self, label = label, action = action, open = open or…
user1632861
3
votes
2 answers

Lua the __ipairs function implemented does not end the loop

I made an empty table, and using ipairs to access it will call other functions, but I don't know how to end the loop, when the loop proceeds to the last element, it still continues local arr = {} arr = setmetatable(arr, { __len = function(tbl) …
Rider
  • 91
  • 4
3
votes
1 answer

Can LUA meta tables assist in detection of nilling object?

I was wondering if you can detect the nilling of an object through meta tables ? foo = {} foo_mt = { __newindex = function (t, k, v) print (k, v) rawset (t, k, v) end } setmetatable (foo, foo_mt) foo ['oof'] =…
Aardvark
  • 608
  • 5
  • 15
3
votes
1 answer

lua tables, simplest way to overload __tostring

Thanks to all the Lua stackoverflow folks who have discussed how to customized printing tables. After much reading, I post the following and ask the Lua gurus.... is this the simplest way? is it too simple (i.e. broken in some way I don't…
Tim Menzies
  • 641
  • 5
  • 14
3
votes
1 answer

Metatable is not indexed, even though setmetatable is used

According to the Lua manual, setmetatable still works the same as in Lua 5.0. Yet for some reason, when I try this code in Lua 5.1.5 and 5.3.1, it appears that the metatable is not accessed: ClassTable = {} ClassTable.getString = function(self) …
stands2reason
  • 672
  • 2
  • 7
  • 18
3
votes
1 answer

Adding _concat to numbers to create number ranges - am I mad?

Just as a random experiment I'm considering adding a __concat() metamethod to the number metatable (usually a new metatable as numbers don't seem to have metatables by default?). The idea is that I could do something like 3..5 and get back 3, 4,…
cabbageforall
  • 620
  • 1
  • 5
  • 12
3
votes
0 answers

Lua setfenv/metatable sandbox won't work with ROBLOX methods

In ROBLOX Lua, I'm writing a game that involves users creating and running Lua scripts. Obviously, I need to prevent the use of certain services and functions, such as the Kick function on the Player class, or anything related to DataStore or…
NotAshley
  • 142
  • 2
  • 11
3
votes
3 answers

How do we change the way print displays a table

Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2. So far I've tried setting the…
No Name
  • 116
  • 7
3
votes
2 answers

lua: how do I make getmetatable() return nil?

I create a userobject in C, and as part of this I create a metatable to override methods like __tostring, __index, etc. I noticed, though, that it's possible to retrieve the object's metatable through getmetatable(). It seems that it's possible to…
Tom
  • 653
  • 1
  • 8
  • 15
3
votes
1 answer

Lua metatable, how to forward undefined functions?

I have an empty table which I want to act as a "gateway" to another set of functions at another location. tbl = {} I want to pass called functions from this table to somewhere else as a string: tbl.someMethod("hello") I've tried this with limited…
FortuneCookie101
  • 505
  • 1
  • 7
  • 19
3
votes
1 answer

Inherit a metatable (class) and use its required constructor parameter

I found this tutorial: http://lua-users.org/wiki/InheritanceTutorial I've got a metatable called Creature. Creature requires an argument in its constructor. The required argument is a string that represents the name. local creature =…
Evan
  • 67
  • 4
3
votes
1 answer

Why does this cause a C stack overflow?

I'm aware that I can fix this problem by using rawset, but I'm just wondering why the following code causes a C stack overflow. local mt = { __newindex = function(self, key, value) self[key] = value end } local x =…
David
  • 693
  • 1
  • 7
  • 20
3
votes
2 answers

Lua metatables: class fields vs instance fields

I'm working on a game in Corona/Lua, and implementing a class called "Item" which represents an in game weapon, armor, amulet, etc. And I'm new to object-oriented Lua. After creating a new instance of the class, I'm finding that setting certain…
Aaron
  • 1,031
  • 10
  • 17
3
votes
2 answers

Lua override # for strings

I'm trying to imlement my own length method for strings in Lua. I have successfully overriden len() method for string, but I have no idea how to do this for # operator. orig_len = string.len function my_len(s) print(s) return…
Xanx
  • 545
  • 1
  • 5
  • 17
1 2
3
12 13