411 REFERENCE · Wikibooks
BlitzMax/Modules/System/Lua scripting
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
The MaxLua module provides a way to use the Lua scripting language from within Blitzmax programs.
Lua is a simple but fast and powerful scripting language. For more information on programming in Lua, please visit the official Lua site at http://www.lua.org
Here is an example of the MaxLua module in action:
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
Types
TLuaObject
A Lua 'objectMethods
Init
Invoke
Functions
Create
TLuaObject: Methods
Init
Method Init:TLuaObject( class:TLuaClass,supr:Object )Description: Initialize the Lua objectInformation:
Sets the object's class and super object.
If the object was created with the TLuaObject.Create function, you do not need to call
this method.
Invoke
Method Invoke:Object( name$,args:Object[] )Description: Invoke an object methodInformation:name should refer to a function within the object's classes' source code.
TLuaObject: Functions
Create
Function Create:TLuaObject( class:TLuaClass,supr:Object )Description: Create a Lua objectInformation''':
Once a lua object has been created, object methods (actually Lua functions defined in the
class) can be invoked using the Invoke method.
TLuaClassA Lua 'classThe TLuaClass type is used to contain Lua source code.
The source code should consist of a series of one or more Lua functions.
To actually invoke these functions a lua object must first be created - see TLuaObject.
Methods
SourceCode
SetSourceCode
Functions
Create
TLuaClass: Methods
SourceCode
Method SourceCode$()Description: Get source codeReturns: The Lua source code for the class.
SetSourceCode
Method SetSourceCode:TLuaClass( source$ )Description: Set source codeInformation:
Sets the class source code.
If the class was created with the TLuaClass.Create function, you do not need to call this
method.
TLuaClass: Functions
Create
Function Create:TLuaClass( source$ )Description: Create a Lua classReturns: A new Lua class object.Information:
The source parameter must be valid Lua source code, and should contain a series of one or
more lua function definitions.
These functions can later be invoked by using the TLuaObject.Invoke method.
Functions
LuaRegisterObject
Function LuaRegisterObject( obj:Object,name$ )Description: Register a global object with LuaInformation:
Once registered, the object can be accessed from within Lua scripts using the name''' identifier.