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

LUA基本應用

開發(fā) 前端
游戲中的界面都是用LUA來寫,這個和WOW是一模一樣的,嗯,以后再慢慢研究.年終了,保存下先.

游戲中的界面都是用LUA來寫,這個和WOW是一模一樣的,嗯,以后再慢慢研究.年終了,保存下先.

lua語言規(guī)則:

  1. lua_State *L = lua_open(); //建立一個LUA狀態(tài)機   
  2. luaopen_base(L); // 啟動它   
  3. const char *buf = "print('hello, world!')";   
  4. lua_dostring(buf); //把buf寫入到lua中并執(zhí)行   
  5. lua_close(L); // 關閉L   
  6. lua_pushstring(L, "var"); //將變量的名字放入棧]   
  7. lua_getglobal(L, "var"); //變量的值現(xiàn)在棧頂   
  8. int var = lua_tonumber(L, -1); //取得棧頂?shù)脑?nbsp;  
  9. lua_tostring( ls, -1 ); //取得棧頂?shù)牡脑? 一般用于參數(shù)傳遞   
  10. lua_pushstring( ls, s_szPlayer ); //把某個字符串元素壓入棧中,可用于參數(shù)傳出   
  11. lua_pushnumber(L, 200); //把某個數(shù)字元素壓入棧中,   
  12. lua_register(L, "foo", foo); 
  13.  //把自己在C++中編寫的函數(shù)foo拿到lua中注冊,這樣在lua腳本中就可以調用這個函數(shù) 

 在Lua中,函數(shù)等同于變量,所以你可以這樣來取得這個函數(shù):

  1. lua_getglobal(L, "main");//函數(shù)現(xiàn)在棧頂 

現(xiàn)在,我們可以調用這個函數(shù),并傳遞給它正確的參數(shù):

  1. lua_pushnumber(L, 100); //將參數(shù)壓棧   
  2. lua_pcall(L, 1, 1, 0); //調用函數(shù),有一個參數(shù),一個返回值   //返回值現(xiàn)在棧頂   
  3. int result = lua_tonumber(L, -1); 

例子:

  1. #include "lua.h" 
  2.  #include "lauxlib.h" 
  3.  #include "lualib.h" 
  4.  int foo(lua_State *L) 
  5.  { 
  6.  //首先取出腳本執(zhí)行這個函數(shù)時壓入棧的參數(shù) 
  7.  //假設這個函數(shù)提供一個參數(shù),有兩個返回值 
  8.  //get the first parameter 
  9.  
  10.  const char *par = lua_tostring(L, -1); 
  11.  printf("%s\n", par); 
  12.  //push the first result 
  13.  
  14.  lua_pushnumber(L, 100); 
  15.  //push the second result 
  16.  
  17.  lua_pushnumber(L, 200); 
  18.  //return 2 result 
  19.  
  20.  return 2; 
  21.  } 
  22.  int main(int argc, char *argv[]){ 
  23.  lua_State *L = lua_open(); 
  24.  luaopen_base(L); 
  25.  luaopen_io(L); 
  26.  lua_register(L, "foo", foo); 
  27.  const char *buf = "r1, r2 = foo("hello") print(r1..r2)"
  28.  lua_dostring(L, buf); 
  29.  lua_close(L); 
  30.  return 0; 
  31.  } 

原文鏈接:http://tech.it168.com/j/2008-02-17/200802171011561.shtml

責任編輯:陳四芳 來源: it168.com
相關推薦

2010-07-08 15:24:17

SNMP trap

2010-03-03 16:40:55

Python HTTP

2010-02-25 10:52:29

WCF響應服務

2010-02-26 13:40:28

WCF消息頭

2010-03-04 14:57:08

Python解密VBS

2010-02-02 14:45:35

C++ typeof

2010-03-03 14:30:05

Python set類

2010-03-03 14:40:37

Python打包方法

2010-03-04 09:27:34

調用Python腳本

2010-03-03 10:03:55

Python連接Sql

2010-02-25 18:04:02

WCF IIS宿主

2010-03-01 15:40:04

WCF實例停用

2010-03-01 11:24:31

WCF面向服務

2010-03-01 16:04:31

WCF服務契約

2010-03-01 09:48:23

WCF會話服務

2010-02-23 15:58:57

WCF Session

2010-03-03 16:08:26

Python取得文件列

2010-02-06 17:27:03

C++ replace

2010-03-05 15:47:59

Python Stri

2010-03-04 15:52:59

Python構造列表
點贊
收藏

51CTO技術棧公眾號