主頁(yè) > 知識(shí)庫(kù) > Lua腳本調(diào)用外部腳本

Lua腳本調(diào)用外部腳本

熱門(mén)標(biāo)簽:上海企業(yè)外呼系統(tǒng)排名 400手機(jī)電話(huà)免費(fèi)辦理 百度地圖標(biāo)注位置網(wǎng)站 開(kāi)通400電話(huà)申請(qǐng)流程 智能語(yǔ)音電銷(xiāo)的機(jī)器人 揚(yáng)州電銷(xiāo)外呼系統(tǒng)軟件 如何利用高德地圖標(biāo)注家 武漢百應(yīng)人工智能電銷(xiāo)機(jī)器人 電腦外呼系統(tǒng)輻射大嗎

test.lua腳本定義main函數(shù)如下:

function main(szName, num1, num2)   
  print("main()", szName, num1, num2); 
  local nRandMax = 10000;  
  local nRand = math.random(nRandMax); 
  print("nRand =", nRand) 
  return 1; 
end

現(xiàn)在我想在test.lua腳本中調(diào)用另外一個(gè)test1.lua腳本文件中的GetRandMax(),通常的做法是這樣的:

function main(szName, num1, num2)   
  print("main()", szName, num1, num2); 
  dofile("test1.lua")   --加載并編譯運(yùn)行腳本 
  local nRandMax = GetRandMax();  
  local nRand = math.random(nRandMax); 
  print("nRand =", nRand) 
  return 1; 
end

看上去是這樣的,非常簡(jiǎn)單,通過(guò)dofile對(duì)test1.lua文件進(jìn)行加載并編譯運(yùn)行,這部操作會(huì)將test1.lua文件中的函數(shù)和變量壓入全局的虛擬棧中,這樣就可以實(shí)現(xiàn)對(duì)GetRandMax()調(diào)用。但是這里有個(gè)問(wèn)題:dofile函數(shù)接受的是需要引用的外部腳本的路徑,如果多次調(diào)用會(huì)不勝其煩,而且效率不高??赡苡腥苏l(shuí)說(shuō),那我們直接把dofile作為全局函數(shù)執(zhí)行,就像這樣:

dofile("test1.lua") 
dofile("test2.lua") 
dofile("test3.lua") 
 
function main(szName, num1, num2)   
  print("main()", szName, num1, num2); 
  local nRandMax = GetRandMax();  --調(diào)用test1.lua中的函數(shù) 
  local nRand = math.random(nRandMax); 
  print("nRand =", nRand) 
  return 1; 
end

這樣做是可以的,但是仍然有些問(wèn)題:

    1,如果test.lua文件dofile(test1.lua),而test1.lua文件dofile(test.lua),怎么辦!出現(xiàn)循環(huán)應(yīng)用,執(zhí)行后堆棧溢出;

復(fù)制代碼 代碼如下:

test.lua:5: too many C levels (limit is 200) in function at line 5 near '"main()"'

    2,dofile的路徑問(wèn)題,每次都要傳入文件的絕對(duì)路徑,如果路徑不正確,無(wú)法正常執(zhí)行,這也是個(gè)很煩的事。

復(fù)制代碼 代碼如下:

cannot open \script\test.lua: No such file or directory

那怎樣做才更好呢?既然dofile有這些的問(wèn)題,我們就把這些問(wèn)題解決。在C/C++中實(shí)現(xiàn)Include腳本接口函數(shù),在腳本中作為全局函數(shù)調(diào)用,實(shí)現(xiàn)外部腳本文件包含功能。

//腳本接口 TLua_Funcs g_GameFunc[] = { 
  { "Include",        LuaInclude }, 
  { "ReloadAllScript",    LuaReloadAllScript}, 
  { "SayHello",       LuaSayHello }, 
  { "StopGame",       LuaStopGame }, 
};

在Include函數(shù)中使用set集合避免重復(fù)包含的問(wèn)題,獲取當(dāng)前的執(zhí)行路徑拼接成絕對(duì)路徑,這樣就省了不少事;)

Include("\\script\\test1.lua") --Include腳本接口 
 
function main(szName, num1, num2)   
  print("main()", szName, num1, num2); 
  local nRandMax = GetRandMax();  --調(diào)用test1.lua中的函數(shù) 
  local nRand = math.random(nRandMax); 
  print("nRand =", nRand) 
  return 1; 
end

您可能感興趣的文章:
  • Cocos2d-x中調(diào)用Lua及HelloWorld.lua源碼分解
  • C++中調(diào)用Lua函數(shù)實(shí)例
  • Lua中調(diào)用C++函數(shù)實(shí)例
  • Lua中的閉合函數(shù)、非全局函數(shù)與函數(shù)的尾調(diào)用詳解
  • Lua中調(diào)用函數(shù)使用點(diǎn)號(hào)和冒號(hào)的區(qū)別

標(biāo)簽:宜賓 新余 延邊 黑龍江 江西 張掖 嘉峪關(guān) 武漢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua腳本調(diào)用外部腳本》,本文關(guān)鍵詞  Lua,腳本,調(diào)用,外部,Lua,腳本,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Lua腳本調(diào)用外部腳本》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Lua腳本調(diào)用外部腳本的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章