metatable

Hello metatable.__len world

a 夏天 提交于 2020-01-04 04:36:25
问题 A beginner's question about Lua and metatables , with a example as simple as an Hello‑World, involving the len event, which unfortunately does not returns the expected result (I'm using Lua 5.1 installed from Ubuntu's official repository). The case Here is the example: Test_Type = {}; function Test_Type.__len (o) return 1; end; function new_test () local result = {}; setmetatable(result, Test_Type); return result; end; do local a_test = new_test(); print (#a_test); print(getmetatable(a_test).

Lua - Why are C functions returned as userdata?

≡放荡痞女 提交于 2019-12-24 08:31:00
问题 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 may use self to refer to both. This is how I do my binding in C# in the Player class: state.NewTable("Player"); // Create Player wrapper table state["Player.data"] = this; // Bind Player.data to the Player class state.NewTable("mt"); // Create temp

Lua metatable class destructor

核能气质少年 提交于 2019-12-14 02:18:25
问题 I have the following Lua metatable class, how can I add a destructor to it so when a certain condition arrives it will destruct the created object and set it's value to nil? ------------------------------------------------- -- Arrow class ------------------------------------------------- local arrow = {} local arrow_mt = { __index = arrow } -- metatable function arrow.new(x, y) -- constructor local newArrow = { position = { x = x, y = y } } return setmetatable( newArrow, arrow_mt ) end

Lua userdata: Unable to have simultaneous array access and methods

好久不见. 提交于 2019-12-10 23:58:37
问题 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 is in Lua, and I attempted a C implementation which seems inelegant, but regardless, it creates a new problem in that my new methods can no longer take arguments, and I get this error: attempt to call method 'asTable' (a table value) on this Lua

How do we change the way print displays a table

南楼画角 提交于 2019-12-10 22:07:10
问题 Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2 . So far I've tried setting the __tostring metamethod but that doesn't seem to be invoked by print . Is there some metamethod I've been missing or does the answer have nothing to do with metamethods? 回答1: __tostring works: aTable = {aValue=1} local mt = {__tostring = function(t) local

Lua override # for strings

旧街凉风 提交于 2019-12-10 20:25:32
问题 I'm trying to imlement my own length method for strings in Lua. I have successfully overriden len() method for string, but I have no idea how to do this for # operator. orig_len = string.len function my_len(s) print(s) return orig_len(s) end string.len = my_len abc = 'abc' If I call: print(abc:len()) It outputs: abc 3 But print(#abc) Outputs only '3' and that means it called original length function instead of mine. Is there a way to make # call my length function? 回答1: I'm trying to imlement

what is actual implementation of lua __pairs?

心不动则不痛 提交于 2019-12-07 02:30:38
问题 Does anybody know actual implementation of lua 5.2. metamethod __pairs ? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs() ? I need to override __pairs and want to skip some dummy variables that I add in a table. 回答1: The following would use the metatable meta to explicitly provide pairs default behavior: function meta.__pairs(t) return next, t, nil end Now, for skipping specific elements, we must replace the returned next :

what is actual implementation of lua __pairs?

岁酱吖の 提交于 2019-12-05 06:06:49
Does anybody know actual implementation of lua 5.2. metamethod __pairs ? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs() ? I need to override __pairs and want to skip some dummy variables that I add in a table. The following would use the metatable meta to explicitly provide pairs default behavior: function meta.__pairs(t) return next, t, nil end Now, for skipping specific elements, we must replace the returned next : function meta.__pairs(t) return function(t, k) local v repeat k, v = next(t, k) until k == nil or theseok(t,

Lua metatables and metamethod - How to call a different member function

南笙酒味 提交于 2019-12-04 04:49:53
问题 I have the following Class local PROGRESS = {} PROGRESS.__index = function(self,key) if key~="__group" and self.__group[key] then return self.__group[key] else return rawget(self,key) end end What this does is when You access table[key] it performs a lookup in table.__group (which is an object of another class) and returns table.__group[key] ,if it is not nil. Now I am trying to do the same for member functions. i.e If I call table:key() a lookup must be performed in table.__group and if the