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

__tostring in custom classes Lua

The following code should print 'hello', however it is printing the memory location of the table (i.e. 'table: 052E67D0'). Please, explain what I'm missing here. TestClass = {} function TestClass:new(o) o = o or {} setmetatable(o, self) …
2
votes
1 answer

How to change a table's metatable but inherit it's own methods in Lua

in Lua we do OO programming like this: MyClass = {} function MyClass:new() local obj = {} setmetatable(obj, self) self.__index = self obj.hello = "hello world" return obj end function MyClass:sayHi() …
Webert Lima
  • 13,197
  • 1
  • 11
  • 24
2
votes
1 answer

How can I change every index into a table using a metatable?

I'm trying to write a metatable so that all indexes into the table are shifted up one position (i.e. t[i] should return t[i+1]). I need to do this because the table is defined using index 1 as the first element, but I have to interface with a…
multipleinterfaces
  • 8,913
  • 4
  • 30
  • 34
2
votes
1 answer

table.insert doesn't trigger __index?

I made a custom table using metatables that automatically tracks sizing when elements are added. It works very well and removes the need for the # operator or the getn function. However it has a problem. If you call table.insert, the function…
Hatefiend
  • 3,416
  • 6
  • 33
  • 74
2
votes
2 answers

Can you compare the "types" of tables or metatables in Lua?

I am calling an API function in the Lord of the Rings Online (LOTRO) Beta Lua scripting feature. The API method returns a "type" called ClassAttributes that will be on of the given Class Attribute "types". I say "types" because when I call type()…
John B
  • 20,062
  • 35
  • 120
  • 170
2
votes
1 answer

Lua - __newindex metamethod for existing index?

I recently learned the existence of methatables in lua, and i was toying around with them until an idea came to my mind: would it be possible to use those to try and avoid "duplicates" in a table? I searched and searched and so far couldn't find…
Thex
  • 31
  • 1
  • 5
2
votes
1 answer

Lua complex number class arithmetic

I have some lua code implemented with the standard binary operators ==,>,<,-,+,*,etc. I want to add some functionality with a lua object like imaginary numbers (not specifically imaginary numbers, but an answer with them in mind would still be…
CircArgs
  • 570
  • 6
  • 16
2
votes
0 answers

-Lua 5.1- Overriding __metatable using rawget/rawset in a restricted environment

I am currently testing a strict custom environment in lua that takes advantage of _G and sets it to a locked metatable,I then uses the __call metamethod of that metatable to sandbox any scripts I need to. This restricts access to most functions and…
Batoda
  • 33
  • 3
2
votes
1 answer

Trying to make a lua proxy with metatables

I've read about metatables (here and here) and I asked myself if I could create a function call proxy by just adding a function with the same name to the __index table, so that it calls the index function (mine), and then I can call the normal…
K0IN
  • 79
  • 1
  • 7
2
votes
1 answer

Can Lua PCall invoke __call metafunction when I want to call a table from c

I am trying call a table using c API and Lua5.1. I'm doing it by following steps: create a table "mt" that has __call metafunction create a table "newT" and set "mt" to "newT" metatable pcall "newT" My problem is at step 3, I get the error:…
2
votes
1 answer

Lua Meta Event Argument Order

I'm using the Lua API to override meta-events for my own C++ objects wrapped as userdata. However, some meta-events can take multiple arguments that may be userdata or regular values which I can convert to a userdata. For example, __add, __eq,…
Lars
  • 233
  • 2
  • 9
2
votes
1 answer

Lua userdata: Unable to have simultaneous array access and methods

I had this guy's problem: Lua userdata array access and methods wherein when I set the __index of my userdata's metatable, it always called the getter, instead of my other methods that weren't declared for meta-events. The solution to the above link…
Lars
  • 233
  • 2
  • 9
2
votes
1 answer

Difference in Lua metatable __index positioning

I keep seeing two ways of defining the __index on a metatable: Account = {} Account.__index = Account function Account.create(balance) local self = { balance = balance } return setmetatable(self, Account) end Or: Account = {} function…
tyrondis
  • 3,364
  • 7
  • 32
  • 56
2
votes
1 answer

attaching metatables within tabes

I have parser that parses a config file and produces a table. The resulting table can look something like: root = { global = { }, section1 = { subsect1 = { setting = 1 subsubsect2 = { } } } } The goal is to have a table I…
hookenz
  • 36,432
  • 45
  • 177
  • 286
2
votes
1 answer

Metatables, attempt to call method 'rename' (a nil value)

This is my first time using metatables, I did a simple script to test in Lua demo but it always give me "attempt to call method 'rename' (a nil value)", why? peds = {} function peds.new ( name ) local tb = { name = name } setmetatable ( tb,…
t3wz
  • 23
  • 2