脚本宝典收集整理的这篇文章主要介绍了Lua编程示例(六): C语言调用Lua函数,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
C++端:
#include "stdafx.h" lua_state *L; void load_lua(lua_State **L,char *filename){ *L=luaL_newstate(); luaL_oPEnlibs(*L); if(luaL_loaDFile(*L,filename) || lua_pcall(*L,0,0,0)){ luaL_error(*L,"load file error! %s",lua_tostring(*L,-1)); } } int _tmain(int argc, _TCHAR* argv[]) { load_lua(&L,"raw.lua"); //此处若直接传入L会出错 lua_getglobal(L,"gettable"); if(lua_pcall(L,0,1,0) !=0){ luaL_error(L,"pcall wrong %s",lua_tostring(L,-1)); } luaL_checktype(L,1,LUA_TTABLE); int n=lua_objlen(L,1); PRintf("n = %d\n",n); lua_pushstring(L,"ee"); lua_rawseti(L,1,5); //t[n]=v,n为第三个参数,v是栈顶元素 n=lua_objlen(L,1); printf("n = %d\n",n); int i; for(i=1;i<=n;i++){ lua_rawgeti(L,1,i); printf("%s\n",lua_tostring(L,-1)); } return 0; }
lua脚本:
function gettable() tb={ "aa","bb","cc","dd"} return tb end
运行输出的结果为:
n = 4 n = 5 aa bb cc dd ee
以上是脚本宝典为你收集整理的Lua编程示例(六): C语言调用Lua函数全部内容,希望文章能够帮你解决Lua编程示例(六): C语言调用Lua函数所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。