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
0
votes
0 answers

How to serialize a metatable in Lua?

local binser = require "binser" local log, floor, ceil, min, random = math.log, math.floor, math.ceil, math.min, math.random local makeNode = function(value,size) return { value=value, next={}, width={}, …
Takoyaki
  • 11
  • 2
0
votes
1 answer

Adding/Removing MetaColumn from MetaTable

I'd like to add/remove a column from MetaTable. But table.Column is of type ReadOnlycollection. How can I modify, add or remove a column from the table? Thank you.
Kamyar
  • 18,639
  • 9
  • 97
  • 171
0
votes
1 answer

Lua - How would I break meta tables or make them unuseable?

So in my game most exploiters are using metatables to stop bans and I would like to break them, I dont use them and it would only hurt the exploiters. Even if this breaks other parts of lua I can and will fix it but this needs to stop and this is my…
James
  • 83
  • 6
0
votes
1 answer

lua metatables - first parameter in __index function

I'm trying to learn metatables in Lua and I came across the following example: - local my_metatable = {} local my_tab = {} setmetatable(my_tab, my_metatable) -- Set the __index metamethod: my_metatable.__index = function (tab, key) …
Zalokin
  • 55
  • 6
0
votes
1 answer

LuaJ Vector Class

I am trying to create a Vector class for use with LuaJ. The end goal is to have the user not write much lua, and have most of the work done on my Java engine. As my understanding goes, I need to set the metatable for the lua representation of my…
orange451
  • 21
  • 4
0
votes
1 answer

Lua5.2: Torch tutorial gives an __index based Stack Overflow

I have been wanting to get into torch and started with this tutorial. However I ran into a stack overflow when running the code specifically with the setmetatable function. I believe that this is happening because of the large 50000 image input file…
0
votes
1 answer

How to create MetaTable from Entity Framework table

How to create MetaTable from Entity Framework table or context? I am using code-first. This is intended for use with some DynamicData methods
monkeyhouse
  • 2,875
  • 3
  • 27
  • 42
0
votes
1 answer

How to use __index as a function?

I'm trying to imitate: b = {1,2,3} a = setmetatable({1,nil,3},{__index = b}) print(a[2]) -- prints 2 with that: b = {1,2,3} a = setmetatable({1,nil,3},{__index = function(t,k) rawget(b,k) end}) print(a[2]) -- nil What did I do wrong?
Parki
  • 159
  • 9
0
votes
1 answer

Lua "attempt to index a nil value"

I'm trying to register a vector type with Lua, but I'm getting a strange "attempt to index a new value" error when I'm calling the addition metafunction from Lua. Here is the code section involved. I did not include any other metafunctions (they…
Therhang
  • 825
  • 1
  • 9
  • 15
0
votes
1 answer

LuaJit - Get metatable from module/package and assign it to userdata

Say I have this metatable for a custom struct vector2_t which is inside a module mymod like this: local mymod = {} local ffi = require("ffi") local C = ffi.C ffi.cdef[[ typedef struct { double x; double y; }…
SLC
  • 2,167
  • 2
  • 28
  • 46
0
votes
2 answers

Lua objects tables leaking between object instances, but not records

I'm trying to learn Lua, so hopefully this is an easy question to answer. The following code does not work. The variable childContext leaks between all instances of classes. --[[-- Context class. --]]-- CxBR_Context = {} -- Name of…
Dennis Miller
  • 952
  • 1
  • 9
  • 22
0
votes
1 answer

Lua C API code not calling __newindex functions but calling other functions

I have written some code to separate registering custom functions and the __newindex and __index functions into 2 separate functions. The goal of my code is to have functions and variables visible to the Lua script writer that are organized based…
0
votes
1 answer

When using Metatables with Lua, is there a way for me to determine if the value is being set or being retrieved?

I've set up a "psuedo-oop" system inside of my script that lets me better interact with the UserData from my application. My system works remarkably good except that it doesn't pass references, it passes values. Because of this, if I say something…
Freesnöw
  • 30,619
  • 30
  • 89
  • 138
0
votes
3 answers

does Lua allow metamethods with weird number of arguments?

For example, can I declare a metamethod for __index which takes two arguments, then do something like myuserdata[somearg1, somearg2]? Not that I want to use it or that I have any idea of why it would be useful, I'm just wondering if in my library…
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
-2
votes
1 answer

Call metatable methods inside metatable itself

Is there a way to call metatable methods inside the metatable itself? For example local t = {} local mt = { __index = { dog = function() print("bark") end, sound = function() t:dog() end …
user3204810
  • 323
  • 6
  • 16
1 2 3
12
13