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

Is it possible to bypass __tostring the way rawget/set bypasses __index/__newindex in Lua?

For example: local my_table = { name = "my table" } local my_table_mt = {} function my_table_mt.__tostring(tbl) return "%s<%s>":format(tbl.name or "?", rawtostring(tbl)) end Is something like this possible? I know the rawtostring method…
Llamageddon
  • 3,306
  • 4
  • 25
  • 44
4
votes
1 answer

Removing Metatables from a table in Lua

I want to "unhook" a metatable from a table and was wondering if: tbl = setmetatable(tbl, false) -- or nil is the correct way to do this? I could not find any info about how to do it correctly. Do I need to use an assignment operator? Also, would…
Mayron
  • 2,146
  • 4
  • 25
  • 51
4
votes
1 answer

Override Assignment Operator in Lua with C API

I have objects in my C++ program that I pass to Lua as userdata, and I override the metatable for this userdata so that assignments to and from indices of the object (via __newindex and __index) result in a call to C, which converts the assignment…
Lars
  • 233
  • 2
  • 9
4
votes
2 answers

How to understand metatable in Lua?

I have used Python, but now I'm learning Lua because of Torch. The word 'metatable' is really hard to understand for me. For example, is metatable a special kind of table? How does it change the behavior of table?
Bruce Zhang
  • 95
  • 1
  • 6
4
votes
1 answer

Lua, which way to do class-table and instantiation?

The question originated from http://tylerneylon.com/a/learn-lua/ The tutorial includes codes: Dog = {dog1 = 'original dog class'} function Dog.new(self, ... ) newObj = {sound = 'woof'} self.__index = self return setmetatable(newObj,…
Frank He
  • 75
  • 1
  • 8
4
votes
2 answers

Lua __metatable bypass?

I was wondering if there is any way to escape the __metatable metamethod. I know there isn't one, but I'm trying to do something like this, but obviously __metatable blocks that from happening: -- pretend that there is a __metatable field given…
4
votes
1 answer

Adding a userdata metatable to a lua table

I've got a scripting system working well using userdata objects. However, I now want to have a property on my userdata that can take a regular table. I think what I should do is create a normal table and set the metatable to use my current set of…
Phil Lello
  • 8,377
  • 2
  • 25
  • 34
4
votes
2 answers

Lua 5.2 - C++ object within an object (using lua_lightuserdata)

edit: [SOLUTION IN ANSWER 2] I am new to LUA and am having trouble trying to do what I want. I have a C++ object that looks like this: C++ Object definitions struct TLimit { bool enabled; double value; TLimit() : enabled(false),…
4
votes
1 answer

How to fix this unpack issue?

I'm creating an Array class that adds more usage to tables. I have a metamethod that allows me to combine two tables, ex: Array(5) .. Array(6, 10) should give you {5, 6, 10} I'm aware that I can use two loops to do this, but I'm trying to make my…
David
  • 693
  • 1
  • 7
  • 20
4
votes
1 answer

Metatable issue

So I know that lua will look to a table's metatable if it doesn't contain the variable I reference, but it seems wrong that when I attempt to set a variable that doesn't exist yet in a table it sets it in the metatable instead. Heres an example of…
Saana
  • 43
  • 2
4
votes
1 answer

modify # operator in lua

I have made a lua console on the love2d engine which is irrelevant. I am trying to upgrade the metatables to be able to effect more things (the pairs function, change the metatable on another table instead of the targeted one etc.) and one of the…
4
votes
1 answer

Adding Barewords to Lua

To implement a domain specific language, within lua, I want to add barewords to the language. So that print("foo") could be written as print(foo) The way I have done this is by changing the metatable of the enviroment table _G. mt = {__index =…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
4
votes
1 answer

Confused about Lua metatables on page 108 of Programming in Lua

I'm studying Lua, using the book Programming in Lua, first edition. I'm having trouble understanding metatables. This is the code and explanations that appear on page 108: Set = {} function Set.new (t) local set = {} for _, l in ipairs(t) do…
maxam
  • 159
  • 1
  • 9
4
votes
2 answers

Hiding a Lua metatable and only exposing an object's attributes

How do you create a Lua object that only exposes its attributes and not its methods? For example: local obj = { attr1 = 1, attr2 = 2, print = function(...) print("obj print: ", ...) end, } Produces: > for k,v in pairs(obj) do print(k,…
Sean
  • 9,888
  • 4
  • 40
  • 43
4
votes
1 answer

What purpose do metatables serve in Lua (5.2) for OOP?

I'm struggling to understand how metatables work and why they are needed in Lua for creating classes and for inheritance. Every OOP example I find for Lua is a little different from the last, but they always use metatables, specifically for the…
Joe S
  • 75
  • 1
  • 6
1
2
3
12 13