C/C++读取Lua中的变量及调用Lua函数
2016-07-06 18:16
411 查看
config.lua
运行结果
print('--In Lua--') local a = -10 print('--math.abs(a)--',math.abs(a)) width = 1080 height = 720 name = "iphone" numStr = 12345 function test1() print('--function test1--') end function test2(var) print('--function test2--') print('--var--',var) end function test3() print('--function test3--') return "调用了lua function test3" end function test4(var) print('--function test4--') local num = tonumber(var) num = num + 10 return num end function test5(var) print('--function test5--') local t = 10 var = var + 100 return t , var end function test6(x,y) print('--function test6--') print('--x--y--',x,y) return x + y end
// // LuaEngine.h // LuaAndCpp // #ifndef __LuaAndCpp__LuaEngine__ #define __LuaAndCpp__LuaEngine__ #include <stdio.h> #include <iostream> #include "lua.hpp" class LuaEngine { public: ~LuaEngine(); static LuaEngine* getInstance(); static void destroyInstance(); // 读取Lua中的变量 void getConfigWidthAndHeight(int* width,int* height); // 调用Lua中的函数 无参无返回值 void callLuaFunction1(); // 调用Lua中的函数 无返回值 有一个参数 void callLuaFunction2(); // 调用Lua中的函数 无参数 有一个返回值 void callLuaFunction3(); // 调用Lua中的函数 有一个参数 有一个返回值 void callLuaFunction4(); // 调用Lua中的函数 有一个参数 有多个返回值 void callLuaFunction5(); // 调用Lua中的函数 有多个参数 有一个返回值 void callLuaFunction6(); private: LuaEngine():m_pLuaState(NULL){}; void init(); std::string getFilePath(std::string fileName); lua_State* m_pLuaState; }; #endif /* defined(__LuaAndCpp__LuaEngine__) */
// // LuaEngine.cpp // LuaAndCpp // #include "LuaEngine.h" LuaEngine::~LuaEngine(){ lua_close(m_pLuaState); } static LuaEngine* LuaEngine_instance = NULL; LuaEngine* LuaEngine::getInstance(){ if (LuaEngine_instance == NULL) { LuaEngine_instance = new LuaEngine(); LuaEngine_instance->init(); } return LuaEngine_instance; } void LuaEngine::destroyInstance(){ if (LuaEngine_instance) { delete LuaEngine_instance; LuaEngine_instance = NULL; } } void LuaEngine::init(){ m_pLuaState = luaL_newstate(); luaL_openlibs(m_pLuaState); luaL_dofile(m_pLuaState, this->getFilePath("config.lua").c_str()); // lua_pcall(m_pLuaState, 0, 0, 0); } std::string LuaEngine::getFilePath(std::string fileName){ return "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/scripts/" + fileName ; } void LuaEngine::getConfigWidthAndHeight(int* pWidth,int* pHeight){ // 获取Lua文件中的全局变量 lua_getglobal(m_pLuaState, "width"); // -1 栈顶位置 lua_isnumber 检测是否能转换成数字类型 if (lua_isnumber(m_pLuaState, -1)) { int width = (int)lua_tonumber(m_pLuaState, -1); std::cout << "width = " << width << std::endl; *pWidth = width; } lua_pop(m_pLuaState, 1); lua_getglobal(m_pLuaState, "height"); if (lua_isnumber(m_pLuaState, -1)) { int height = (int)lua_tonumber(m_pLuaState, -1); std::cout << "height = " << height << std::endl; *pHeight = height; } lua_pop(m_pLuaState, 1); lua_getglobal(m_pLuaState, "name"); if (lua_isstring(m_pLuaState, -1)) { char* str = (char*)lua_tostring(m_pLuaState, -1); std::cout << "name = " << str << std::endl; } lua_pop(m_pLuaState, 1); // Lua中的 numStr 是 number 类型 // lua_isstring 不是判断是不是字符串类型,而是检测是否能转换成字符串类型 lua_getglobal(m_pLuaState, "numStr"); if (lua_isstring(m_pLuaState, -1)) { char* str = (char*)lua_tostring(m_pLuaState, -1); std::cout << "numStr = " << str << std::endl; } lua_pop(m_pLuaState, 1); } // 调用Lua中的函数 无参无返回值 void LuaEngine::callLuaFunction1(){ lua_getglobal(m_pLuaState, "test1"); // 0 参数 0 返回值 int rel = lua_pcall(m_pLuaState, 0, 0, -1); if (rel == -1) { std::cout << "callLuaFunction1 error \n"; } } // 调用Lua中的函数 无返回值 有一个参数 void LuaEngine::callLuaFunction2(){ lua_getglobal(m_pLuaState, "test2"); lua_pushstring(m_pLuaState, "调用了function test2"); int rel = lua_pcall(m_pLuaState, 1, 0, -1); if (rel == -1) { std::cout << "callLuaFunction2 error \n"; } } // 调用Lua中的函数 无参数 有一个返回值 void LuaEngine::callLuaFunction3(){ lua_getglobal(m_pLuaState, "test3"); int rel = lua_pcall(m_pLuaState, 0, 1, -1); if (rel == -1) { std::cout << "callLuaFunction3 error \n"; } char* str = (char*)lua_tostring(m_pLuaState, -1); std::cout << "lua function test3 返回值 -- " << str << std::endl; } // 调用Lua中的函数 有一个参数 有一个返回值 void LuaEngine::callLuaFunction4(){ lua_getglobal(m_pLuaState, "test4"); // 传参 lua_pushnumber(m_pLuaState, 10); int rel = lua_pcall(m_pLuaState, 1, 1, -1); if (rel == -1) { std::cout << "callLuaFunction4 error \n"; } int num = (int)lua_tonumber(m_pLuaState, -1); std::cout << "lua function test4 返回值 -- " << num << std::endl; } // 调用Lua中的函数 有一个参数 有多个返回值 void LuaEngine::callLuaFunction5(){ lua_getglobal(m_pLuaState, "test5"); lua_pushnumber(m_pLuaState, 10); int rel = lua_pcall(m_pLuaState, 1, 2, -1); if (rel == -1) { std::cout << "callLuaFunction5 error \n"; } int t = (int)lua_tonumber(m_pLuaState, -2); int var = (int)lua_tonumber(m_pLuaState, -1); std::cout << "lua function test5 返回值 -- t , var " << t << " " << var << std::endl; } // 调用Lua中的函数 有多个参数 有一个返回值 void LuaEngine::callLuaFunction6(){ lua_getglobal(m_pLuaState, "test6"); lua_pushnumber(m_pLuaState, 10); lua_pushnumber(m_pLuaState, 13); int rel = lua_pcall(m_pLuaState, 2, 1, -1); if (rel == -1) { std::cout << "callLuaFunction6 error \n"; } if (lua_isnumber(m_pLuaState, -1)) { int num = (int)lua_tonumber(m_pLuaState, -1); std::cout << "x+y== " << num << std::endl; lua_pop(m_pLuaState, 1); } }
// // main.cpp // LuaAndCpp // #include <iostream> #include "LuaEngine.h" int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; LuaEngine* luaEngine = LuaEngine::getInstance(); int width = 0; int height = 0; luaEngine->getConfigWidthAndHeight(&width,&height); std::cout << "getConfigWidthAndHeight = " << width << " " << height << std::endl; luaEngine->callLuaFunction1(); luaEngine->callLuaFunction2(); luaEngine->callLuaFunction3(); luaEngine->callLuaFunction4(); luaEngine->callLuaFunction5(); luaEngine->callLuaFunction6(); LuaEngine::destroyInstance(); return 0; }
运行结果
Hello, World! --In Lua-- --math.abs(a)-- 10 width = 1080 height = 720 name = iphone numStr = 12345 getConfigWidthAndHeight = 1080 720 --function test1-- --function test2-- --var-- 调用了function test2 --function test3-- lua function test3 返回值 -- 调用了lua function test3 --function test4-- lua function test4 返回值 -- 20 --function test5-- lua function test5 返回值 -- t , var 10 110 --function test6-- --x--y-- 10 13 x+y== 23 Program ended with exit code: 0
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨