在Lua中注册C ++函数? [英] Register C++ function in Lua?

查看:179
本文介绍了在Lua中注册C ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Lua中注册一个c ++函数。



但遇到此错误:

  CScript.cpp |错误:类型的参数'int(CScript ::)(lua_State *)'不匹配'int(*)(lua_State *)'| 

编辑:

 code> 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)
{
//我们需要至少一个参数
int n = lua_gettop );
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); 


解决方案

然而,存在的用于容易地将C ++代码与Lua相关联的任何各种机制将允许在Lua中使用仅仅基本的Lua C API的Lua中的C ++非静态成员函数。



<你这样做。 toLua ++ SWIG Luabind 等。如果您认真对待使用C ++对象与Lua,我建议挑选其中之一,并使用它,而不是编写自己的版本。我个人使用Luabind(大多数时候; SWIG在工具箱中的位置),因为它是没有某种形式的代码生成的。它完全是在C ++中完成的,因此没有生成C ++源文件的预传递步骤。


I am trying to register a c++ function in Lua.

But getting this error:

CScript.cpp|39|error: argument of type 'int (CScript::)(lua_State*)' does not match 'int (*)(lua_State*)'|

EDIT:

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;
}

And in main:

lua_register(L, "setDisplayMode", Lua_SetDisplayMode);

解决方案

You cannot directly register a C++ non-static member function in Lua using just the basic Lua C API.

However, any of the various mechanisms that exist for easily associating C++ code with Lua will allow you to do so. toLua++, SWIG, Luabind, etc. If you're serious about using C++ objects with Lua, I suggest picking one of those and using it, rather than writing your own version. I personally use Luabind (most of the time; SWIG has its place in the toolbox), as it is the one that doesn't have some form of code generation. It's all done purely in C++, so there's no pre-pass step that generates a C++ source file.

这篇关于在Lua中注册C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆