Tag: metatable

Lua – 为什么C函数作为userdata返回?

我正在为我的引擎开发游戏脚本,并使用metatable将函数从表(存储自定义函数和播放器数据)重定向到userdata对象(这是我的Player类的主要实现),以便用户可以用self来指代两者。 这就是我在Player类中使用C#进行绑定的方法: state.NewTable(“Player”); // Create Player wrapper table state[“Player.data”] = this; // Bind Player.data to the Player class state.NewTable(“mt”); // Create temp table for metatable state.DoString(@”mt.__index = function(self,key) local k = self.data[key] if key == ‘data’ or not k then return rawget(self, key) elseif type(k) ~= ‘function’ then print(type(k)) print(k) return k else return function(…) […]

Lua userdata:无法同时访问arrays和方法

我有这个人的问题: Lua userdata数组访问和方法 其中当我设置userdata的metatable的__index时,它总是调用getter,而不是我没有为meta-events声明的其他方法。 上面链接的解决方案是在Lua中,我尝试了一个看似不优雅的C实现,但无论如何,它创建了一个新问题,因为我的新方法不能再接受参数,我得到了这个错误: attempt to call method ‘asTable’ (a table value) 在这个Lua声明中: print_r(c:asTable() ) 这是我设置一切的方式: //Methods, many of which are overridden Lua meta-events (with the underscores) static const struct luaL_reg vallib_m [] = { {“asTable”, PushLuaTable}, //these functions are not called {“asCopy”, CopyLuaVal}, {“__newindex”, SetLuaVal}, {“__index”, GetLuaVal}, {“__tostring”, ValToString}, {“__gc”, GarbageCollectVal}, {“__metatable”, HideMetaTable}, […]