C++中通过LUA API访问LUA脚本变量学习教程

移动开发 iOS
C++中通过LUA API访问LUA脚本变量学习是本文要介绍的内容,主要是来学习一些关于栈操作、数据类型判断的LUA API,可以使用这些函数获得脚本中的变量值。

C++中通过LUA API访问LUA脚本变量学习是本文要介绍的内容,主要是来学习一些关于栈操作、数据类型判断的LUA API,可以使用这些函数获得脚本中的变量值。

1、步骤

编写 test01.lua 脚本,在VS2003中创建控制台C++程序并正确配置,执行查看结果,修改test02.lua脚本后查看执行结果

2、测试脚本

以下是用来测试的lua脚本

  1. function plustwo(x)      
  2.       local a = 2;      
  3.       return x+a;  
  4. end;  
  5. rows = 6;  
  6. cols = plustwo(rows); 

上面的脚本定义了一个函数、两个全局变量(LUA脚本变量默认是全局的)。之后的C++程序中,我们将通过栈操作获得这两个变量 rows, cols

3、控制台程序

  1. #include <iostream> 
  2.  
  3. extern "C"  
  4. {  
  5.     #include "lua.h"  
  6.     #include "lauxlib.h"  
  7.     #include "lualib.h"  
  8. }  
  9.  
  10. using namespace std;  
  11.  
  12. int main(int argc, char* argv[])  
  13. {  
  14.     cout << "01_Read_Stack" << endl;  
  15.  
  16.     /**//* Create a LUA VMachine */  
  17.     lua_State *L = lua_open();  
  18.     luaopen_base(L);  
  19.     luaopen_table(L);  
  20.     luaL_openlibs(L);  
  21.     luaopen_string(L);  
  22.     luaopen_math(L);  
  23.  
  24.     int iError;  
  25.     iError = luaL_loadfile(L, "../test01.lua");  
  26.     if (iError)  
  27.     {  
  28.         cout << "Load script FAILED!" << lua_tostring(L, -1)<< endl;  
  29.         lua_close(L);  
  30.         return 1;  
  31.     }  
  32.     iError = lua_pcall(L, 0, 0, 0);  
  33.     if (iError)  
  34.     {  
  35.         cout << "pcall FAILED"<< lua_tostring(L, -1)<< iError<< endl;  
  36.         lua_close(L);  
  37.         return 1;  
  38.     }  
  39.       
  40.     lua_getglobal(L, "rows");  
  41.     lua_getglobal(L, "cols");  
  42.  
  43.     if (!lua_isnumber(L, -2))  
  44.    {  
  45.         cout << "[rows] is not a number" << endl;  
  46.         lua_close(L);  
  47.         return 1;  
  48.     }  
  49.     if (!lua_isnumber(L, -1))  
  50.     {  
  51.         cout << "[cols] is not a number" << endl;  
  52.         lua_close(L);  
  53.         return 1;  
  54.     }  
  55.     cout << "[rows]"  
  56.          << static_cast<int> (lua_tonumber(L, -2))  
  57.          << "[cols]"  
  58.          << static_cast<int> (lua_tonumber(L, -1))  
  59.          << endl;  
  60.  
  61.     lua_pop(L,2);  
  62.     lua_close(L);  
  63.     return 0;  

小结:C++中通过LUA API访问LUA脚本变量学习教程的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2013-12-13 16:53:00

Lua脚本语言C++

2013-12-13 16:46:18

Lua脚本语言

2011-08-22 17:25:31

LuaC++函数

2011-09-06 17:12:25

Lua脚本C++封装库

2011-08-23 16:48:41

Lua 5.1API 函数

2011-08-23 13:54:10

LUA全局变量

2011-08-24 13:27:07

Lua 游戏C接口脚本

2011-08-24 10:32:03

LuaPlusC++Lua

2013-12-13 15:48:52

Lua脚本语言

2013-12-13 15:42:32

Lua脚本语言

2011-08-23 09:50:29

LuaPlusLua 脚本

2011-08-23 16:37:05

Lua数学库

2011-08-25 16:20:33

Lua脚本变量

2011-08-22 18:08:09

Lua脚本

2011-09-01 16:45:15

J2MELua

2011-08-23 13:27:46

Luaglobal变量

2011-08-23 17:11:13

Lua事件C#

2011-08-24 17:09:35

LUA闭包函数

2011-08-22 17:13:00

LuaC++函数

2011-08-23 15:34:56

Lua模式 匹配
点赞
收藏

51CTO技术栈公众号