自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++中通過(guò)LUA API訪問(wèn)LUA腳本變量學(xué)習(xí)教程

移動(dòng)開(kāi)發(fā) iOS
C++中通過(guò)LUA API訪問(wèn)LUA腳本變量學(xué)習(xí)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)一些關(guān)于棧操作、數(shù)據(jù)類型判斷的LUA API,可以使用這些函數(shù)獲得腳本中的變量值。

C++中通過(guò)LUA API訪問(wèn)LUA腳本變量學(xué)習(xí)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)一些關(guān)于棧操作、數(shù)據(jù)類型判斷的LUA API,可以使用這些函數(shù)獲得腳本中的變量值。

1、步驟

編寫(xiě) test01.lua 腳本,在VS2003中創(chuàng)建控制臺(tái)C++程序并正確配置,執(zhí)行查看結(jié)果,修改test02.lua腳本后查看執(zhí)行結(jié)果

2、測(cè)試腳本

以下是用來(lái)測(cè)試的lua腳本

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

上面的腳本定義了一個(gè)函數(shù)、兩個(gè)全局變量(LUA腳本變量默認(rèn)是全局的)。之后的C++程序中,我們將通過(guò)棧操作獲得這兩個(gè)變量 rows, cols

3、控制臺(tái)程序

  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;  

小結(jié):C++中通過(guò)LUA API訪問(wèn)LUA腳本變量學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 博客園
相關(guān)推薦

2013-12-13 16:53:00

Lua腳本語(yǔ)言C++

2013-12-13 16:46:18

Lua腳本語(yǔ)言

2011-08-22 17:25:31

LuaC++函數(shù)

2011-09-06 17:12:25

Lua腳本C++封裝庫(kù)

2011-08-24 13:27:07

Lua 游戲C接口腳本

2011-08-23 16:48:41

Lua 5.1API 函數(shù)

2011-08-23 13:54:10

LUA全局變量

2011-08-24 10:32:03

LuaPlusC++Lua

2011-08-23 09:50:29

LuaPlusLua 腳本

2011-08-23 16:37:05

Lua數(shù)學(xué)庫(kù)

2013-12-13 15:42:32

Lua腳本語(yǔ)言

2013-12-13 15:48:52

Lua腳本語(yǔ)言

2011-09-01 16:45:15

J2MELua

2011-08-22 18:08:09

Lua腳本

2011-08-25 16:20:33

Lua腳本變量

2011-08-23 13:27:46

Luaglobal變量

2011-08-23 17:11:13

Lua事件C#

2011-08-24 17:09:35

LUA閉包函數(shù)

2011-08-22 17:13:00

LuaC++函數(shù)

2011-08-24 14:14:13

LUA環(huán)境 配置
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)