C++调用lua函数(带参)

C++程序

void main()
{
	lua_State *L = luaL_newstate();
	if (L == NULL)
	{
		return 1;
	}
	luaL_openlibs(L);

	int ret = luaL_dofile(L, "main.lua");
		
	printf("top = %d \n", lua_gettop(L));
	lua_getglobal(L,"event");//获取函数名
	lua_pushstring(L,"key");//压入一个值
	if (lua_pcall(L, 1, 0, 0) != 0)//参数数量,函数返回值,错误输出函数(lua的函数,地址)
	{
		printf("c++ call lua function error %s\n",lua_tostring(L,-1));//返回错误,将错误压入到栈顶
		lua_pop(L,1);//若有错误则弹出
	}	
	printf("top = %d \n", lua_gettop(L));
}

lua程序:

function event(q)
 print("c++ call lua function event")
 print("q = "..q)
end

运行结果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章