LUA scripting for BMX

BlitzMax Forums/BlitzMax Programming/LUA scripting for BMX

EDIT: Check out the module tweaks forum for this module and samples.

Hi People,

As you may know, I mentioned I'd be working on a LUA module for BMX (free for everyone to use). I have now successfully executed a LUA script through BMX and while it's still a little early, it's working great! Best of all it's cross-platform...

Here is some code from the sample I'm putting together. Note that this sample uses the interface to C directly and therefore looks a little scary for Blitz people. I'll be wrapping this into a nice script engine for all of us to use for AI, Missions logic etc:
Strict

Import Pub.lua

' Call our entry point
main()

Function main()

	Local lua_state			' hold info about the current state of lua
	DebugStop

	' Initialize lua
	lua_state = lua_open()
	
	' Open libs
	luaopen_base(lua_state)
	luaopen_table(lua_state)
	luaopen_io(lua_state)
	luaopen_string(lua_state)
	luaopen_math(lua_state)
	
	Local script:String = "print (~qHello LUA World!~q)"
	
	' Execute!
	Local error:Int = luaL_loadbuffer(lua_state, script.ToCString(), script.length, "testscript") Or ..
						lua_pcall(lua_state, 0, 0, 0)
						
	' Check for error
	If (error) Then
		RuntimeError ("Error: " + lua_tostring(lua_state, -1))
		lua_pop(lua_state, -1)
	End If
	
	' Shut down lua
	lua_close(lua_state)

End Function