BlitzMax/Modules/System/Lua scripting

From Wikibooks, open books for an open world
Jump to navigation Jump to 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 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[edit | edit source]

TLuaObject[edit | edit source]

A Lua 'object'

Methods
  • Init
  • Invoke
Functions
  • Create

TLuaObject: Methods[edit | edit source]

Init

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.

Invoke

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 | edit source]

Create

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.

TLuaClass[edit | edit source]

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.

Methods
  • SourceCode
  • SetSourceCode
Functions
  • Create

TLuaClass: Methods[edit | edit source]

SourceCode

Method SourceCode$()

Description: Get source code

Returns: The Lua source code for the class.

SetSourceCode

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 | edit source]

Create

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.

Functions[edit | edit source]

LuaRegisterObject[edit | edit source]

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.