Why LUA?
With lua it is easy possible to create an addon/mod-api, for example world of warcraft use lua for all interface-addons. You can also create more complex configuration-files (with calculations, if then, depending on screen resolution and so on).
How to use lua?
you need the "module_lua.pbi", "module_lua_extern.pbi" and lua (lua53.dll for Win64, lua53_x86.dll for Win32 or liblua53.so for Linux). I downloaded the lua-binaries from http://luabinaries.sourceforge.net/index.html .
the module is designed to work with creation of DLLs. You must load the lua with
Code: Select all
XIncludeFile "module_lua.pbi"
UseModule lua
If Lua_Initialize()=#False
End
EndIf
you can unload lua with
Code: Select all
Lua_Dispose()
Code: Select all
XIncludeFile "..\module_lua.pbi"
UseModule lua
ProcedureC MyPrint(*L)
Protected i.i
Protected max=lua_gettop(*L)
For i=1 To max
If lua_isstring(*l,i)
Debug "Lua Print | " + lua_tostring(*L, i)
ElseIf luaL_callmeta(*l,i,"__tostring")
Debug "Lua Print | " +lua_tostring(*l,-1)
EndIf
Next
ProcedureReturn 0
EndProcedure
If Lua_Initialize("..\")=#False ; the lua.dll is placed a folder under the example. When the dll is at the same place, call simple Lua_Initalize()
End
EndIf
lua=luaL_newstate() ; Create a new lua-vm, it is possible to have more than one.
luaL_openlibs(LUA) ; initalize the default librarys
lua_register(LUA, "print", @MyPrint()) ; Replace print, so it output to the debug
;load lua-code from datasection, compile it and then start
If luaL_loadbuffer(lua,?mainfile,?end_mainfile-?mainfile-1,"Example") Or lua_pcall(LUA, 0, #LUA_MULTRET, 0);IMPORTANT remove the last 0!
Debug "Lua Error|" + lua_tostring(LUA, -1)
lua_pop(LUA, 1)
EndIf
;Call the function add with two parameters
lua_getglobal(lua,"add")
lua_pushnumber(lua,1.5);lua-numbers are double
lua_pushnumber(lua,2.1)
If lua_pcall(lua,2,1,0)
Debug "Error in add:"+lua_tostring(lua,-1)
lua_pop(LUA, 1)
EndIf
;output
Debug "lua-add:"+lua_tonumber(lua,-1)
;same with integer-values
lua_getglobal(lua,"add")
lua_pushinteger(lua,3);lua integer are quads
lua_pushinteger(lua,6)
If lua_pcall(lua,2,1,0)
Debug "error in add:"+lua_tostring(lua,-1)
lua_pop(LUA, 1)
EndIf
Debug "lua-add:"+lua_tointeger(lua,-1)
;close the lua-vm
Lua_close(lua):lua=0
;unload dll
Lua_Dispose()
End
DataSection
mainfile:
;IncludeBinary "example1.lua"
Data.a ~"print (\"Output over pure basic Debug\") \n"+
~"function add (a,b) \n"+
~" return a+b \n"+
~"end" ; Important - a zero is added! Must be removed with loadBuffer
end_mainfile:
EndDataSection
Important: The code is not complete tested. Also I don't have a linux-installation and I haven't found a MacOs lua library. I can only test win32 and win64 - most of the time win64. Use at own risk.