Lua 如何通过名称调用库函数并设置它';s参数

Lua 如何通过名称调用库函数并设置它';s参数,lua,lua-api,Lua,Lua Api,我的C代码中定义了一个库函数: static const struct luaL_reg SelSurfaceLib [] = { {"CapabilityConst", CapabilityConst}, {"create", createsurface}, {NULL, NULL} }; static const struct luaL_reg SelSurfaceM [] = { {"Release", SurfaceRelease}, {"Get

我的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=子项GetPosition() 但是我正在寻找一种方法,通过在meta的函数中设置它来透明地执行

换句话说,我想让HLSubSurface对象从SubSurface继承方法

有什么想法吗

谢谢

洛朗

function HLSubSurface(parent_surface, x, y, sx, sy)
   local srf = parent_surface:SubSurface(x, y, sx, sy)

   local self = {
      -- fields
      ....
   }

   setmetatable(self, {__index = 
      function (obj, key)
         local parent_field
         local parent_fields = getmetatable(srf).__index
         if type(parent_fields) == "function" then
            parent_field = parent_fields(key)
         elseif parent_fields then 
            parent_field = parent_fields[key]
         end
         if type(parent_field) == "function" then
            return 
               function(o, ...)
                  if o == obj then
                     return parent_field(srf, ...)
                  else
                     return parent_field(o, ...)
                  end
               end
         else
            return parent_field
         end
      end
   })

   return self
end
你的主要代码是:

sub = HLSubSurface( parent, 0,0, 160,320 )
x,y = sub:GetPosition()

要明确的是,您希望调用
sub.somemethod(…)
在幕后变成
srf:somemethod(…)
,其中
srf
是使用
hlf
创建时设置的值?确切地说:)我的目标是避免重复所有调用。感谢您的回复,但我希望避免重新声明库中的所有函数(有几十个:)。是将函数名作为参数传递并调用类似obj.srf:$funcname()?@destroyedlolo-Answer updated的任何方法。所有的父函数都是通过自动替换第一个参数来继承的。好吧,我会退出一周,但回来后,我会测试并反馈。再次感谢。如果没有编程,你如何度过整个星期?