Skip to content

Instantly share code, notes, and snippets.

@nilium
Created March 16, 2009 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/79663 to your computer and use it in GitHub Desktop.
Save nilium/79663 to your computer and use it in GitHub Desktop.
Example of calling Lua functions and Lua calling C functions in BlitzMax
' lua_test.bmx
Strict
Import Axe.Lua
' Available to lua
Function lua_KeyHit( lvm@ Ptr )
' Get the number of arguments to the function
Local top:Int = lua_gettop(lvm)
Local results:Int = 0
' For each argument
For Local idx:Int = 1 To top
' check to see if it's a number- if it isn't, skip it
If lua_type(lvm, idx) <> LUA_TNUMBER Then
Continue
EndIf
' if it's a number, get the result of KeyHit and push it as a bool
Local khRes:Int = KeyHit(Int lua_tonumber(lvm, idx))
lua_pushboolean(lvm, khRes)
' number of results increases
results :+ 1
Next
' return the number of results
Return results
End Function
' Main
Function Main:Int()
' create the VM
Local lua_vm@ Ptr = luaL_newstate()
Local Running:Int = False
If lua_vm = Null Then
Print "Couldn't create new Lua VM"
Return 1
EndIf
' Try to load and execute the file- if it fails, print the error and quit
If luaL_dofile(lua_vm, "main.lua") <> 0 Then
Print "Error: "+lua_tostring(lua_vm, -1)
lua_pop(lua_vm, 1)
lua_close(lua_vm)
Return 1
EndIf
SetGraphicsDriver(GLMax2DDriver())
Graphics(800,600,0,0)
EnablePolledInput()
' Add KeyHit to globals table
lua_pushstring(lua_vm, "KeyHit") ' push the name of the function
lua_pushcclosure(lua_vm, lua_KeyHit, 0) ' push the function pointer
lua_rawset(lua_vm, LUA_GLOBALSINDEX) ' put it in the globals table
Running = True
While Running And (Not AppTerminate())
Cls
' Get the function from the globals table
lua_pushstring(lua_vm, "Update") ' push the name of the function
lua_rawget(lua_vm, LUA_GLOBALSINDEX) ' get it from the globals table
' if the value is a function, proceed to call it, otherwise spit out an error
If lua_type(lua_vm, -1) = LUA_TFUNCTION Then
' otherwise call the function
If lua_pcall(lua_vm, 0, 1, 0) = 0 Then
' If the call succeeded, check to see that the result is a bool
If lua_type(lua_vm, -1) = LUA_TBOOLEAN Then
' Now set whether or not the app will continue running from the function's result
Running = lua_toboolean(lua_vm, -1)
Else
' If it's not a bool, it's an error
Print "Error: Return type of Update was not a boolean"
EndIf
Else
' If the call failed, print the error string from lua_pcall
Print "Error: "+lua_tostring(lua_vm, -1)
EndIf
Else
' If it's not a function, it's an error
Print "Error: Update() is not defined or not a function"
EndIf
' If it isn't a function, pop it. -|
' If it is, we called it and got a result, pop the result. |- awesome
' If an error was pushed, the error is popped. -|
lua_pop(lua_vm, 1)
Flip
Wend
EndGraphics
' close the vm
lua_close(lua_vm); lua_vm = Null
Return 0
End Function
' Run
Return Main()
-- main.lua
function Update()
if KeyHit(27) then
return false
else
return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment