SLua學習紀錄,require 和 module用法

  • 703
  • 0

從別的lua類別呼叫function實作

就算一個代碼被require多次 也只會裝載一次

custom.txt

local t ={}

function t.qq()
	print("Get QQ")
end

function t.tt()
	print("Get TT")
end

return t //一定要做return的動作 不然會取得不到

呼叫 custom 裡面的 function

callCustom.txt

local custom = require “custom”

function main()

   custom.qq()
   custom.tt()

end

或者 custom 調用module函數 做成全局模塊

custom.txt

module("custom",package.seeall)

function qq()
	print("Get QQ")
end

function tt()
	print("Get TT")
end

這樣就不需要做return 也可以呼叫

callCustom.txt

require “custom”

function main()
   custom.qq()
   custom.tt()
end
也可以整合需要呼叫的類別 統一由一個類別輸出

main.txt

require “custom1”

function main()
 custom1.aaa()
 custom2.bbb()
end

custom1.txt

require “custom2”

module(“custom1”,package.seeall)

function aaa()
  print(“This is Custom1”)
end

custom2.txt

module(“custom2”,pagkage.seeall)

function bbb()
	print("This is Custom2")
end

custom1已經裝載custom2 所以只要require custom1 就可以使用custom2 裡面的function

但是在custom2的程式碼裡 必須也要調用全局模塊 否則就會報錯

Log.Error [string "..."]:4: System.Exception: expect string or type table