【发布时间】:2017-06-12 14:37:35
【问题描述】:
我在 C 代码中定义了一个类似的库函数:
static const struct luaL_reg SelSurfaceLib [] = {
{"CapabilityConst", CapabilityConst},
{"create", createsurface},
{NULL, NULL}
};
static const struct luaL_reg SelSurfaceM [] = {
{"Release", SurfaceRelease},
{"GetPosition", SurfaceGetPosition},
{"clone", SurfaceClone},
{"restore", SurfaceRestore},
{NULL, NULL}
};
void _include_SelSurface( lua_State *L ){
luaL_newmetatable(L, "SelSurface");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_register(L, NULL, SelSurfaceM);
luaL_register(L,"SelSurface", SelSurfaceLib);
}
我可以将它与这个 Lua 代码一起使用:
local sub = SelSurface.create()
local x,y = sub:GetPosition()
...
现在,我的难题:我正在使用以下代码
function HLSubSurface(parent_surface, x,y,sx,sy )
local self = {}
-- fields
local srf = parent_surface:SubSurface( x,y, sx,sy )
-- methods
local meta = {
__index = function (t,k)
local tbl = getmetatable(srf)
return tbl[k]
end
}
setmetatable( self, meta )
return self
end
我的主要代码是:
sub = HLSubSurface( parent, 0,0, 160,320 )
x,y = sub.GetPosition()
但是失败了
./HDB/80_LeftBar.lua:19: 'SetFont' 的参数 #1 错误(应为 SelSurface,已获取用户数据)
这是因为我需要提供 srf 作为 GetPosition() 函数的第一个参数...但我完全不知道该怎么做:(
我不想在调用 GetPosition() 时这样做, x,y = sub.GetPosition() 但我正在寻找一种方法来透明地将其设置在 meta 的函数中。
换句话说,我想让 HLSubSurface object 从 SubSurface 继承方法。
有什么想法吗?
谢谢。
劳伦特
【问题讨论】:
-
明确地说,您希望调用
sub.somemethod(..)在幕后变成srf:somemethod(...),srf是使用HLSubSurface创建时设置的任何值? -
完全正确 :) 我的目标是避免重复所有调用。