BlitzMax/Modules/System/Lua scripting

From Wikibooks, open books for an open world
Jump to: navigation, search

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

Here is an example of the MaxLua module in action:


'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"+..
"function goodbye()~n"+..
"print( Demo.SayHello( 'CandyMan' ) )~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



A Lua 'object'

  • Init
  • Invoke
  • Create

TLuaObject: Methods[edit]


Method Init:TLuaObject( class:TLuaClass,supr:Object )

Description: Initialize the Lua object

Information: 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.


Method Invoke:Object( name$,args:Object[] )

Description: Invoke an object method

Information: name should refer to a function within the object's classes' source code.

TLuaObject: Functions[edit]


Function Create:TLuaObject( class:TLuaClass,supr:Object )

Description: Create a Lua object

Information: Once a lua object has been created, object methods (actually Lua functions defined in the class) can be invoked using the Invoke method.


A Lua 'class'

The 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.

  • SourceCode
  • SetSourceCode
  • Create

TLuaClass: Methods[edit]


Method SourceCode$()

Description: Get source code

Returns: The Lua source code for the class.


Method SetSourceCode:TLuaClass( source$ )

Description: Set source code

Information: 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[edit]


Function Create:TLuaClass( source$ )

Description: Create a Lua class

Returns: 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.



Function LuaRegisterObject( obj:Object,name$ )

Description: Register a global object with Lua

Information: Once registered, the object can be accessed from within Lua scripts using the name identifier.