c++ - 在 Lua 中注册 C++ 函数?

标签 c++ lua

我正在尝试在 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/

相关文章:

c++ - 为什么不能直接在stream中使用按位运算符?

.net - 如何在 .NET 应用程序中使用从 Win32 CryptoAPI 生成的 key blob?

c++ - 如何显示 256x256 的网格?

需要 Lua 相对路径

lua - Lua 编程第二版说 ".. is right associative"

lua 元表错误或功能?

c++ - 为什么锁定会减慢顺序文件解析器的速度?

c++ - 使用 C++ 在 OpenGL ES 中显示纹理时出现问题

lua - string.match 抛出错误 : attempt to index field '?' (a string value)

lua - 设置私有(private) luarocks 存储库