跳轉到內容

BlitzMax/Modules/System/Lua 指令碼

來自華夏公益教科書

MaxLua 模組提供了一種從 Blitzmax 程式中使用 Lua 指令碼語言的方法。

Lua 是一種簡單但快速且強大的指令碼語言。有關使用 Lua 進行程式設計的更多資訊,請訪問 Lua 官方網站:http://www.lua.org

以下是如何使用 MaxLua 模組的一個示例

Strict

'Our TDemo type...
'
Type TDemo

Method SayHello$( name$ )
Return "Hello "+name+"! Peace be with you..."
End Method

End Type

'Register a demo object with Lua.
'
'Lua code can now access the object using the identifier "Demo".
'
Local demo:TDemo=New TDemo
LuaRegisterObject demo,"Demo"

'source code to our little Lua program...
'
Local source$=..
"function hello()~n"+..
"print( Demo.SayHello( 'Fredborg' ) )~n"+..
"end~n"+..
"function goodbye()~n"+..
"print( Demo.SayHello( 'CandyMan' ) )~n"+..
"end~n"

'create a Lua 'class' and set it's source code...
'
Local class:TLuaClass=TLuaClass.Create( source )

'Now, create an instance of the class.
'
Local instance:TLuaObject=TLuaObject.Create( class,Null )

'We can no invoke methods of the class.
'
instance.Invoke "hello",Null
instance.Invoke "goodbye",Null

TLuaObject

[編輯 | 編輯原始碼]

Lua '物件'

方法
  • Init
  • Invoke
函式
  • Create

TLuaObject: 方法

[編輯 | 編輯原始碼]
Init

方法 Init:TLuaObject( class:TLuaClass, supr:Object )

描述: 初始化 Lua 物件

資訊: 設定物件的類和父物件。

如果物件是用 TLuaObject.Create 函式建立的,則不需要呼叫此方法。

Invoke

方法 Invoke:Object( name$, args:Object[] )

描述: 呼叫物件方法

資訊: name 應引用物件類原始碼中的一個函式。

TLuaObject: 函式

[編輯 | 編輯原始碼]
Create

函式 Create:TLuaObject( class:TLuaClass, supr:Object )

描述: 建立一個 Lua 物件

資訊: 建立 Lua 物件後,可以使用 Invoke 方法呼叫物件方法(實際上是在類中定義的 Lua 函式)。

TLuaClass

[編輯 | 編輯原始碼]

Lua '類'

TLuaClass 型別用於包含 Lua 原始碼。

原始碼應包含一系列一個或多個 Lua 函式。

要實際呼叫這些函式,必須先建立一個 lua 物件 - 請參閱 TLuaObject.

方法
  • SourceCode
  • SetSourceCode
函式
  • Create

TLuaClass: 方法

[編輯 | 編輯原始碼]
SourceCode

方法 SourceCode$()

描述: 獲取原始碼

返回值: 類的 Lua 原始碼。

SetSourceCode

方法 SetSourceCode:TLuaClass( source$ )

描述: 設定原始碼

資訊: 設定類原始碼。

如果類是用 TLuaClass.Create 函式建立的,則不需要呼叫此方法。

TLuaClass: 函式

[編輯 | 編輯原始碼]
Create

函式 Create:TLuaClass( source$ )

描述: 建立一個 Lua 類

返回值: 一個新的 Lua 類物件。

資訊: source 引數必須是有效的 Lua 原始碼,並且應包含一系列一個或多個 lua 函式定義。

這些函式可以使用 TLuaObject.Invoke 方法來呼叫。

LuaRegisterObject

[編輯 | 編輯原始碼]

函式 LuaRegisterObject( obj:Object, name$ )

描述: 在 Lua 中註冊一個全域性物件

資訊: 註冊後,可以使用 name 識別符號從 Lua 指令碼中訪問該物件。

華夏公益教科書