4
在C++代码:收到任何通知时LUA调用未定义功能
class CWindowUI {
public CWindowUI(const char* title,int width,int height);
.....
};
static int CreateWindow(lua_State *l)
{
int width,height;
char *title;
CWindowUI **winp, *win;
name = (char *) luaL_checkstring(l, 1);
width= lua_tounsigned(l, 2);
height= lua_tounsigned(l, 3);
win = new CWindowUI(title,width,height);
if (win == NULL) {
lua_pushboolean(l, 0);
return 1;
}
winp = (CWindowUI **) lua_newuserdata(l, sizeof(CWindowUI *));
luaL_getmetatable(l, "WindowUI");
lua_setmetatable(l, -2);
*winp = win;
return 1;
}
在Lua代码:
local win = CreateWindow("title", 480, 320);
win:resize(800, 600);
现在我的问题是:
功能CreateWindow
将返回命名对象win
和函数resize
未定义。当我在Lua中调用未定义的函数时,如何获得通知?
该通知应包含字符串"resize"
和参数800,600
。 我想修改源映射未定义功能上的回调函数,但它是不正确。
当编译* .lua文件,卢阿使用哈希映射的C函数,如“__index” LUA功能,所以我要修改可以映射未定义功能为默认功能的散列的一部分,所以我可以设置默认功能来获取它。 – tfzxyinhao
“编译* .lua”是什么意思? lua代码不会被编译,而是被解释/执行。表达式/调用“win:resize(800,600)”的评估发生在当时,代码执行达到了这一点。 – lipp
@tfzxyinhao:“* LUA使用哈希映射像C函数到Lua函数‘__index’*”号你以为* *的方式太低级。不要想到“哈希”等等。有表格,映射到值的键。如何实施是无关紧要的。要紧的是,当Lua尝试,就好像是一个表来访问用户数据,它会调用该用户数据的'__index'元方法。 * *你的工作是为Lua返回适当的函数来调用。期。没有散列,没有通知;只是返回一个函数。 –