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
1
vote
0 answers

Keeping track of table accesses with multiple sub tables?

I'm basing this off the idea that you can use a proxy table in order to track table accesses as seen here: http://www.lua.org/pil/13.4.4.html But what if my data structure wasn't a 1 dimensional table but looked something more like this: table = { …
user1826176
  • 309
  • 2
  • 14
1
vote
1 answer

Lua Metatables for function call trapping

I'm trying to use Lua Metatables to make a prettier interface to some internal C++ functions. Here's my code that's working so far. (my.get and my.set are implemented in C++) function setDefault(t) local mt = { __index = function(tab,k) return…
Roddy
  • 66,617
  • 42
  • 165
  • 277
1
vote
1 answer

Lua Metatables Attempt to Index?

So here is some code I wrote up: local fileFunc = {createFolder, openObj, deleteObj, createTxt, theMenu} setmetatable(fileFunc, mt) function fileSys() local fileAction, code print("The File System") print("You are currently at…
Lee Yi
  • 517
  • 6
  • 17
1
vote
1 answer

How to loop through a table in Corona?

Today I came across Corona SDK after using LÖVE for a while. I have stumbled upon a problem that I cannot seem to fix. The Bullets do spawn but they remain motionless. Is there another alternative to update every Bullet or am I doing something…
BruceTheGoose
  • 63
  • 1
  • 10
1
vote
1 answer

How does setmetatable() work and why the metatable is needed in the linked list in lua

I'm learning how Lua's metatables work in OOP, and I am confused of the code I read from the object orientation tutorial on lua-users wiki. Could someone helps to explain the following questions? Thanks. Question 1: The wiki's explanation: Here we…
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
1
vote
0 answers

Lua custom operators

I am trying to create an operator '!' that returns the print function. I am getting the following error: line 15: attempt to call a number value stack traceback: t.lua:15: in main chunk [C]: ? My code is below: local opTable =…
user3254383
1
vote
2 answers

Nested Lua Metatables in C

In a 3D scene, I have an Object that has a position that I would like to move using Lua. eg. box.position.x = 10 box has a metatable ("Object") and so has position ("Vec"). Object has __newindex and __index set to call C functions NewIndexObject…
1
vote
0 answers

Print all metatable content

I have table: [кнк] = (table) [20-6-2014] = (table) [16] = Steve,Maria [16-6-2014] = (table) [17] = Elice, Hans [18] = Steve, Maria, Hans And i have dirty but working code: function af.info(farm_name,date,time,name_string) …
stillru
  • 11
  • 3
1
vote
2 answers

Lua: Interpreting undefined variable by its name

I have a function foo that can get nil values under certain circumstances, i.e. foo(VarA) while VarA is undefined. This undefined VarA should get interpreted as "VarA" but I can't invoke foo("VarA") because VarA should act as an option param…
user3472162
1
vote
2 answers

Specifying both "methods" and index operator in Lua metatable

I need userdata objects to support both methods and the index operator (with integer indices). Is there a way to achieve this in a common metatable without simulating methods via a function? I tried chaining metatables, by setting…
riv
  • 6,846
  • 2
  • 34
  • 63
1
vote
2 answers

Compare a value with a table by using metatables in Lua

I'm looking for a way in Lua 5.1 to compare with metatables, so I can compare any value with a table. If that value is in the table it returns true, and false if it is not in the table. like the following. if table == string then -- does something…
Termanater13
  • 45
  • 1
  • 8
1
vote
1 answer

Error while useing a Method of a Metatable 'attempt to call method (a nil value)'

I get the error 'attempt to call method 'Dot' (a nil value)' while running the following code: -- Vector2 Class Vector2 = {X = 0, Y = 0, Magnitude = 0, Unit = nil} function Vector2.new(XValue,YValue) local Tmp = {} setmetatable(Tmp,Vector2) …
Weeve Ferrelaine
  • 683
  • 1
  • 7
  • 12
1
vote
2 answers

Lua Metatables - calling functions with colon syntax

I have the following problem, somebody can help me? comp = {} comp.__index = function(obj,val) if val == "insert" then return rawget(obj,"gr")["insert"] end return rawget(obj, val) end comp.new = function() local ret = {} …
cbeltrangomez
  • 412
  • 2
  • 9
1
vote
2 answers

Lua OOP function is not being called?

I'm using Lua and LuaBridge with Allegro 5. I decided to port all graphic objects to Lua, but I'm encountering some problems: Character class that is called once Character = {sprite; AI} function Character:new() o = o or {} -- works! …
M89
  • 71
  • 1
  • 6
1
vote
2 answers

Corona SDK how to remove an object?

Well i have some trouble removing an object from the game, the thing is that i have a player class (made out of a metatable), inside it i have a variable called sprite that holds the address for the image sprite i will draw onScreen, so when i…
ershin69
  • 96
  • 3
  • 16