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

Lua: Expression from metatable returns nil instead of false

I have a metatable set up like this example: setmetatable(self, { __index = function(_,k) return Class[k] and Class[k](self, ref) or ref[k] end }) And my function: function Class:isDriving(person) return (person.onRoad and…
Richard Avalos
  • 555
  • 5
  • 18
1
vote
1 answer

Lua: extending userdata in metatables

I am trying to find a proper solution for some protected employee userData that I want extend with additional entries/data for easy access Here's a rough example of what I'm trying to do: This works, but I dont like that I have to put all the custom…
Richard Avalos
  • 555
  • 5
  • 18
1
vote
2 answers

Lua - Why are C functions returned as userdata?

I'm working on game scripting for my engine and am using a metatable to redirect functions from a table (which stores custom functions and data for players) to a userdata object (which is the main implementation for my Player class) so that users…
tayoung
  • 411
  • 1
  • 4
  • 16
1
vote
0 answers

Spring batch doesnt execute drop script

Im tryin to configure spring batch to run schema and drop…
Sue
  • 2,380
  • 2
  • 10
  • 11
1
vote
1 answer

Emulating c++ 'using namespace' via _ENV

if I have a file foo.lua: local foo = {} foo.add = function(a, b) return a+b end foo.sub = function(a, b) return a-b end foo.multiply = function(a, b) return a*b end return foo and in bar.lua I make heavy use of code from foo.lua I am bothered by…
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
1
vote
0 answers

LuaJ overwrite .new()

I am trying to overwrite the .new() lua function(method?) with LuaJ. I tried to set a "new" key in the metatable for my table, but when calling .new() in lua it is ignored and runs the default java-backed function of creating the table as a java…
orange451
  • 21
  • 4
1
vote
1 answer

Print a table without metatables in Lua

Is it possible to print a table without using metatables in Lua? In Roberto's book Programming in Lua, he mentions "The function print always calls tostring to format its output". However, if I override tostring in my table, then I get the…
johnnyodonnell
  • 1,838
  • 3
  • 16
  • 34
1
vote
1 answer

Setting a meta table: Advantage of reference vs inline?

I was wondering if it would make sense to pass a meta table by reference vs declaring it in-line in setmetatable() when you want to use the same meta table for multiple tables. My goal is to save memory, but only if it really makes a significant…
Forivin
  • 14,780
  • 27
  • 106
  • 199
1
vote
1 answer

What is the difference between lua_getmetatable and luaL_getmetatable

Lua API has a function lua_getmetatable which will fetch the table with metafunctions if the value has one. Lua auxiliary library (which is part of lua API) has another function luaL_getmetatable which is a macro that will fetch a value from…
Teivaz
  • 5,462
  • 4
  • 37
  • 75
1
vote
1 answer

lua which operators can be overloaded

i know that it is possible to overload the addition operator in lua for tables. By doing: foo = { value = 10 } bar = { value = 15 } mt = { __add = function(left,right) left.value = left.value + right.value; return left; …
pear
  • 1
  • 3
1
vote
1 answer

Lua: Querying the name of the metatable of a userdata object

I want to query the name of the metatable of some object. Consider that I have some metatable registered as the following: Object obj; // some C object luaL_newmetatable(lua, "my_metatable"); // it's empty lua_pushlightuserdata(lua,…
jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
1
vote
1 answer

Lua, 2-dim. array metatable getting value when "nil"

local A = {{16}, {4,10}, {4,4,6}, {nil,2,-2,4}} -- nil local n = #A local G = {} local mt = {} mt.__index = function(self, i) -- when it goes throw for-loop it brakes when value in "A" is **nil** -- also…
Srdjan M.
  • 3,310
  • 3
  • 13
  • 34
1
vote
2 answers

Lua getmetatable() a locked table

In the lua programming language, I know that you can lock the metatable of userdata with the metamethod __metatable so that no-one can view the userdata's metatable with the getmetatable() function. But, I still want to access that metatable after…
Jack G
  • 4,553
  • 2
  • 41
  • 50
1
vote
1 answer

Lua _G metatable not working

I am trying to control the interactive environment. Here is my try: home: lua Lua 5.0.3 Copyright (C) 1994-2006 Tecgraf, PUC-Rio > for n in pairs(_G) do io.write(n)…
user3015347
  • 503
  • 3
  • 12
1
vote
2 answers

Metamethods and Classes

I created a similar function to what is shown here, and am having trouble with the __add metamethod. I want to be able to use the __add metamethod on two instances of a Class, but the only way it seems to work is if I add the metamethods to the…
eliw00d
  • 321
  • 2
  • 12