佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > C++中調(diào)用Lua配置文件和響應(yīng)函數(shù)示例

C++中調(diào)用Lua配置文件和響應(yīng)函數(shù)示例

熱門標(biāo)簽:呼叫中心市場需求 服務(wù)外包 地方門戶網(wǎng)站 AI電銷 百度競價(jià)排名 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) Linux服務(wù)器

Lua是腳本語言,最大的優(yōu)勢就是輕巧靈便,不用編譯。當(dāng)C的框架寫好,只要更改lua的相應(yīng)處理即可以更改功能,并且不用重新編譯。以下是在C中調(diào)用Lua資源方法的示例程序:
 
C++端:

// Lua1.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。 
// 
 
#include "stdafx.h" 
#includestdio.h> 
 
extern "C" { //如不用extern會出現(xiàn)連接錯誤,編譯成了C++文件 
#include lua.h> 
#include lualib.h> 
#include lauxlib.h> 
} 
 
 
void load (lua_State *L,int *width,int *height){ 
  lua_getglobal(L,"width");  //獲得Lua中變量的值,將其放入棧中 
  lua_getglobal(L,"height"); 
 
  if(!lua_isnumber(L,-2))   //棧頂為-1,然后依次減少 
    luaL_error(L,"`width' should be a number\n"); 
 
  if(!lua_isnumber(L,-1)) 
    luaL_error(L,"`height' should be a number\n"); 
  *width = (int)lua_tonumber(L,-2);  //將棧頂元素轉(zhuǎn)化為數(shù)字 
  *height = (int)lua_tonumber(L,-1); 
} 
 
int add(lua_State *L,int a, int b){  
  int sum=0; 
  lua_getglobal(L,"add");  //函數(shù)也是變量,所以一個取法 
  lua_pushnumber(L,a);   //將變量傳入 
  lua_pushnumber(L,b);   
    //第二個參數(shù)為調(diào)用函數(shù)的參數(shù)的個數(shù),第三個為返回值的個數(shù),第四個參數(shù)是錯誤處理函數(shù),正常運(yùn)行返回0 
  if(lua_pcall(L,2,1,0) != 0){   
    luaL_error(L,"the lua load error! %s",lua_tostring(L,-1)); 
  } 
     //函數(shù)的返回值已經(jīng)傳入棧中 
  if(!lua_isnumber(L,-1)){  
    luaL_error(L,"the func run error! %s",lua_tostring(L,-1)); 
  } 
 
  sum = (int)lua_tonumber(L,-1); //取棧頂元素將其轉(zhuǎn)為數(shù)字,默認(rèn)為double型 
   lua_pop(L,1)    //將return元素移除 
  return sum; 
} 
 
int get_num(lua_State *L){ 
   
  lua_getglobal(L,"roll_num"); 
  if(lua_pcall(L,0,1,0) != 0){ 
    luaL_error(L,"the lua load error! %s",lua_tostring(L,-1)); 
  } 
 
  if(!lua_isnumber(L,-1)){ 
    luaL_error(L,"the func run error! %s",lua_tostring(L,-1)); 
  } 
   int n = (int)lua_tonumber(L,-1); 
  lua_pop(L,1); 
  return n; 
} 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
  lua_State *L = lua_open(); 
  luaL_openlibs(L); //新版本庫添加的方法 
  if(luaL_loadfile(L,"cof.lua") || lua_pcall(L,0,0,0)){ 
    luaL_error(L,"loadfile error! %s \n",lua_tostring(L,-1)); 
  } 
 
  int w=1,h=2; 
  int sum; 
  load(L,w,h); 
  printf("width is %d ,height is %d\n",w,h); 
  sum=add(L,w,h); 
  printf("the sum is: %d \n",sum); 
   
   
  bool flag = true; 
  char c; 
  while(flag){ 
    printf("Do you want to roll the number? \n"); 
    scanf("%c",c); 
    if(c == 'y' || c=='Y'){ 
      printf("rolling the number....\n the num is %d \n",get_num(L)); 
    } 
    else flag=false; 
    getchar(); 
  } 
 
  getchar(); 
  return 0; 
} 


Lua 文件:
 

-- config test 
 
width = 200 
height = 500 
 
function add(a,b) 
  return a+b 
end 
 
math.randomseed(os.time()) 
function roll_num() 
  return math.random(6) 
end 


您可能感興趣的文章:
  • C++讀寫ini配置文件實(shí)現(xiàn)過程詳解
  • 定義vim配置文件vimrc用于c/c++編程
  • C++讀寫INI配置文件的類實(shí)例
  • C++讀取INI配置文件類實(shí)例詳解
  • c++實(shí)現(xiàn)逐行讀取配置文件寫入內(nèi)存的示例
  • C++讀取配置文件的示例代碼

標(biāo)簽:銅川 崇左 蘭州 仙桃 衡水 湘潭 黃山 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C++中調(diào)用Lua配置文件和響應(yīng)函數(shù)示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    霸州市| 宜宾县| 鹤岗市| 沁阳市| 鹰潭市| 静安区| 同德县| 阳东县| 京山县| 碌曲县| 高唐县| 托里县| 商水县| 乌恰县| 宝应县| 田林县| 赞皇县| 汝南县| 江津市| 阳城县| 高淳县| 寿宁县| 咸宁市| 奉节县| 洛阳市| 宁乡县| 蒙山县| 临沂市| 乐业县| 岳西县| 梨树县| 通渭县| 光山县| 元氏县| 富民县| 南通市| 吉安市| 曲周县| 万载县| 会同县| 宝鸡市|