電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
利用lua腳本設計公式

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式初級班:語法及基礎概念
上一篇主題 :: 下一篇主題  
發表人 內容
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2009-7-2, PM 5:48 星期四    文章主題: 利用lua腳本設計公式 引言回覆

先到lua的官網下載lua的lib

接下來是說明的code
這裡的dostring跟dofile是差不多的
只是一個直接從字串取得,另一個從檔案讀入

test這個函數的說明
輸入atk跟def這2個數值
接下來交給腳本
最後取得傳回值

當然你們只需要修改腳本就可以修改公式了



代碼:


#include <iostream>

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#pragma comment(lib,"lualib.lib")

using namespace std;


void main()
{
   // 創建一個指向 Lua 解釋器的指針
   lua_State *L = lua_open();
   
   //函數加載 Lua 庫
   luaL_openlibs(L);

   // 加載 test.lua 腳本
   luaL_dofile(L,"test.lua");

   // 執行字串腳本
    luaL_dostring(L,"function test1() return 100 end");

   // 擷取 Lua 腳本
   //luaL_dofile(L,"test.lua");

   // 擷取一個 Lua 函數 "test1"
   lua_getfield(L, LUA_GLOBALSINDEX, "test1");
   // 傳入 0 個參數 及第 1 個回調
   lua_call(L,0,1);

   // 返回數據
   int a = (int)lua_tonumber(L,1);


   //取得名為 test 的函數
   lua_getfield(L, LUA_GLOBALSINDEX, "test");

   // 輸入2組參數
   lua_pushinteger(L,8); // atk
   lua_pushinteger(L,1); // def
   lua_call(L,2,2);      //輸入2個參數及第二個回調

   int b = (int)lua_tonumber(L,2); //B取得輸出

   // 移除第二個函數的傳入參數,以便下次的呼叫
    lua_pop(L,2);



   // 關閉釋放資源
   lua_close(L);

   cout << "a=" << a << endl;
   cout << "b=" << b << endl;

    //getchar();

   system("PAUSE");
};


test.lua的內容
代碼:

function test(atk,def)
 a = atk
 b = def

 c = a - b

 if c < 0 then
   return 0 
 end

 return c
end


輸出結果
代碼:

a=100
b=7
請按任意鍵繼續 . . .


mirror 在 2009-7-2, PM 8:15 星期四 作了第 1 次修改
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2009-7-2, PM 8:12 星期四    文章主題: 引言回覆

追加一篇寫成函式的應用
有點程度的人應該就不用看了
代碼:


#include <iostream>

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#pragma comment(lib,"lualib.lib")

using namespace std;


int GetHarm(lua_State *L, char *name, int key, int atk, int def)
{
   int b = 0;

   // 取得函數
   lua_getfield(L, LUA_GLOBALSINDEX, name);

   // 輸入2組參數
   lua_pushinteger(L,atk); // atk
   lua_pushinteger(L,def);  // def
   lua_call(L,2,key);       // 輸入2個參數及第二個回調

   b = (int)lua_tonumber(L,key); //B取得輸出

   // 移除第二個函數的參數堆疊,以便下次的呼叫
    lua_pop(L,key);
    //=====================================================

    return b;
}

void main()
{
   // 創建一個指向 Lua 解釋器的指針
   lua_State *L = lua_open();
   
   //函數加載 Lua 庫
   luaL_openlibs(L);

   // 加載 test.lua 腳本
   luaL_dofile(L,"test.lua");

   // 執行字串腳本
    luaL_dostring(L,"function test1() return 100 end");

   // 擷取 Lua 腳本
   //luaL_dofile(L,"test.lua");

   // 擷取一個 Lua 函數 "test1"
   lua_getfield(L, LUA_GLOBALSINDEX, "test1");
   // 傳入 0 個參數 及第 1 個回調
   lua_call(L,0,1);

   // 返回數據
   int a = (int)lua_tonumber(L,1);
   int b = GetHarm(L,"test",2,18,9);
   int c = GetHarm(L,"test",2,27,2);
    //=====================================================




   // 關閉釋放資源
   lua_close(L);

   cout << "a=" << a << endl;
   cout << "b=" << b << endl;
   cout << "c=" << c << endl;

    //getchar();

   system("PAUSE");
};


輸出結果
代碼:

a=100
b=9
c=25
請按任意鍵繼續 . . .
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式初級班:語法及基礎概念 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作