您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x lua调用c++函数

2015-11-24 12:02 423 查看
</pre><pre>


test.h:

extern "C" {
#include "lua.h"
#include "lualib.h"
}

int getString(lua_State *L);


test.cpp:

#include "test.h"

using namespace std;

int getString(lua_State *L)
{
int n = lua_gettop(L);//得到栈中元素个数,其中的元素即是在lua中传的参数

int i;

std::string name;

for(i = 1; i <= n; i++)//循环栈,并取其中的参数,用lua_to...进行类型转换
{
if(!lua_isnumber(L, i))
{
name = lua_tostring(L, i);
}
}

//这时候就可以用取到的参数了,可以用作调用c++函数的参数
//------function();

//将返回的值,压入栈中,用lua_push...进行压栈
lua_pushstring(L, name.c_str());

return 1;//返回一个整数,表示函数返回值的个数,这里返回一个就1
}


AppDelegate.cpp加入:

lua_register(L, "getString", getString);//注册c++函数这样就能用了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: