我正在尝试在 Lua 中注册一个 C++ 函数。
但是得到这个错误:
CScript.cpp|39|error: argument of type 'int (CScript::)(lua_State*)' does not match 'int (*)(lua_State*)'|
编辑:
int CApp::SetDisplayMode(int Width, int Height, int Depth)
{
this->Screen_Width = Width;
this->Screen_Height = Height;
this->Screen_Depth = Depth;
return 0;
}
int CScript::Lua_SetDisplayMode(lua_State* L)
{
// We need at least one parameter
int n = lua_gettop(L);
if(n < 0)
{
lua_pushstring(L, "Not enough parameter.");
lua_error(L);
}
int width = lua_tointeger(L, 1);
int height = lua_tointeger(L, 2);
int depth = lua_tointeger(L, 3);
lua_pushinteger(L, App->SetDisplayMode(width, height, depth));
return 0;
}
主要是:
lua_register(L, "setDisplayMode", Lua_SetDisplayMode);
最佳答案
您不能将类的方法用作普通函数,除非它被声明为static
。您必须定义一个普通函数,它会找出您希望在哪个对象中调用该方法,然后调用该方法。
不可能将类方法用作 C 函数的回调(记住 Lua API 是纯 C 库)的主要原因是计算机不知道该方法应该调用哪个对象上。
关于c++ - 在 Lua 中注册 C++ 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8094992/