C++ Lua 交互练习!!!!!!!!
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ Lua 交互练习!!!!!!!!,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4192字,纯文字阅读大概需要6分钟。
内容图文
// text.cpp : Defines the entry point for the console application. // #include <iostream> #include <fstream> #include <string> usingnamespace std; extern"C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } #pragma comment(lib,"lualib.lib") lua_State * L; LUALIB_API int textC(lua_State *L) { if(L == nullptr) { return0; } cout<<"This msg from C++"<<endl; return0; } LUALIB_API int textC1(lua_State *L) { if(L == nullptr) { return0; } int ax = luaL_checkint(L,-2); int bx = luaL_checkint(L,-1); cout<<"This msg from C++ ax="<<ax<<" bx="<<bx<<endl; lua_pushnumber(L,ax-1); lua_pushnumber(L,bx-1); return2; } void text1() { //---------------------------------------------------------------- cout<<"LUA调用C++函数,带参数"<<endl; lua_pushcfunction(L,textC1); lua_setglobal(L,"textC1"); char* str = "E:\\A.lua"; if (luaL_loadfile(L,str)|| lua_pcall(L,0,0,0)) { constchar* error = lua_tostring(L,-1); cout<< string(error)<<endl; return; } } void text() { //---------------------------------------------------------------- cout<<"LUA调用C++函数"<<endl; lua_pushcfunction(L,textC); lua_setglobal(L,"textC"); //----------------------------------------------------------------char* str = "E:\\A.lua"; if (luaL_loadfile(L,str)|| lua_pcall(L,0,0,0)) { constchar* error = lua_tostring(L,-1); cout<< string(error)<<endl; return; } cout<<"读取LUA中值"<<endl; int a = 0; int b = 0; lua_getglobal(L,"a"); if (!lua_isnumber(L, -1)) { cout << "-2 error" << lua_isnumber(L, -1) << lua_isnumber(L, -1) << endl; return ; } a = lua_tonumber(L,-1); lua_getglobal(L,"b"); if (!lua_isnumber(L, -1)) { cout << "-2 error" << lua_isnumber(L, -1) << lua_isnumber(L, -1) << endl; return ; } b = lua_tonumber(L,-1); cout<<a<<endl; cout<<b<<endl; //------------------------------------------------------------------ cout<<"读取Table中值"<<endl; lua_getglobal(L,"table"); lua_pushstring(L,"a"); lua_gettable(L,-2); //取Table 命令 压入 -2位置 if(lua_isnumber(L,-1)) { cout<<"table.a = "<<lua_tonumber(L,-1)<<endl; } //---------------------------------------------------------------- cout<<"调取LUA中函数"<<endl; lua_getglobal(L,"textlua"); if(!lua_isfunction(L,-1)) { constchar* error = lua_tostring(L,-1); cout<<string(error)<<endl; } if(lua_pcall(L,0,0,0)) { constchar* error = lua_tostring(L,-1); return; } //---------------------------------------------------------------- cout<<"传一个参数"<<endl; lua_getglobal(L,"textlua1"); if(!lua_isfunction(L,-1)) { constchar* error = lua_tostring(L,-1); cout<<string(error)<<endl; } lua_pushstring(L,"111"); if(lua_pcall(L,1,0,0)) { constchar* error = lua_tostring(L,-1); cout<<string(error)<<endl; return; } //---------------------------------------------------------------- cout<<"传一个参数,得到两个返回值"<<endl; lua_getglobal(L,"textlua1"); if(!lua_isfunction(L,-1)) { constchar* error = lua_tostring(L,-1); cout<<string(error)<<endl; } lua_pushstring(L,"111"); if(lua_pcall(L,1,2,0)) { constchar* error = lua_tostring(L,-1); cout<<string(error)<<endl; return; } if(lua_isnumber(L,-1) && lua_isnumber(L,-2)) { constint a = lua_tonumber(L,-2); constint b = lua_tonumber(L,-1); cout<<"the result is "<<a<<","<<b<<endl; } return; } int main ( int argc, char *argv[] ) { cout<<"读取一行LUA"<<endl; char *lua = "print(‘Hello world!‘)"; //创建一个指向lua解释器的指针 L = luaL_newstate(); //加载lua标准库 luaL_openlibs(L); //加载脚本 luaL_loadbuffer(L,lua,strlen(lua),"line"); //运行脚本 text1(); text(); if (LUA_OK!=lua_pcall(L,0,0,0)) { //调用失败,添加处理 } //关闭并释放资源 lua_close(L); //系统命令,暂停 system("pause"); return0; }
-- 测试代码 if textC then textC() else ax,bx = textC1(100,1) print("ax= " .. ax .."bx= " .. bx) end a = 10 b = 11function textlua() print("this is a lua function") endfunction textlua1(value) print("this is a lua function,value = " .. value ) a = 100 b = 200return a,b end table = { a = 123, b = 456 }
原文:http://www.cnblogs.com/Black-Crow/p/5145843.html
内容总结
以上是互联网集市为您收集整理的C++ Lua 交互练习!!!!!!!!全部内容,希望文章能够帮你解决C++ Lua 交互练习!!!!!!!!所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。