Page 1 of 2

LUA - Script with PureBasic

Posted: Mon Jan 02, 2017 7:33 pm
by GPI
http://game.gpihome.eu/PureBasic/lua/

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 now use nearly every lua-c-api-call.
you can unload lua with

Code: Select all

Lua_Dispose()
a more complex example:

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
I have add more example in the 7z.

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.

Re: LUA - Script with PureBasic

Posted: Mon Jan 02, 2017 10:21 pm
by jack
GPI wrote: 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.
Hi GPI
thanks for the code, as for MacOs you can find Lua binaries by rudix.org at http://rudix.org/packages/lua.html
but it should not be too difficult to built the Lua binaries yourself, from terminal: make macosx
:)

Re: LUA - Script with PureBasic

Posted: Thu Feb 23, 2017 7:54 pm
by Xanos
Thank you very much! This is a very nice and clean module and right now I needed exactly that.
For me it is working fine with Win64. Will test Linux later this week.
Unfortunately, I also cannot test it on Mac.

//Edit (although very late): Linux is working absolutely fine as well :-)

Re: LUA - Script with PureBasic

Posted: Fri Feb 24, 2017 2:47 am
by happer66
I'm perhaps not the most mac-savvy kinda guy ( mine's gathering dust in its little corner - it's .... err... an eco-friendly experiment I'm doing, very scientific. Don't ask, it's pretty advanced, so you wouldn't understand :D )

(PB 5.51, MacOS Sierra 10.12.3 x64)

Can happily report that the first example ran fine (only one I tested).

Just change the 'Xincludefile' line in the example so there's a forward slash, I think most modern windows versions handles it just fine so there shouldn't be any need for compiler directives to have the same code running on all OS's regarding that?

Code: Select all

XIncludeFile "../module_lua.pbi"
If you're missing the library (liblua53.dylib), this worked like a charm for quickly making one:
https://blog.spreendigital.de/2015/01/2 ... c-library/

Thanks for the module GDI!

Re: LUA - Script with PureBasic

Posted: Fri Feb 24, 2017 8:24 am
by jack
happer66 wrote: Thanks for the module GDI!
was that spelling auto-correct ?

Re: LUA - Script with PureBasic

Posted: Tue Apr 07, 2020 3:04 pm
by superadnim
link is broken?

Re: LUA - Script with PureBasic

Posted: Tue Apr 07, 2020 3:09 pm
by GG

Re: LUA - Script with PureBasic

Posted: Fri May 01, 2020 4:35 pm
by Joubarbe
Are you sure this is from the same guy?

When running the example, with libraries at the right location, I've got this error:
Image

Re: LUA - Script with PureBasic

Posted: Sun May 03, 2020 5:07 pm
by jack
@Joubarbe
I took a look at the Lua.dll exports and it look like the symbols are there, but from your screenshot it looks like you have Lua.lib in the binaries folder instead of the lib folder, the dll should go either into the same place where your program that uses lua is or you could place it in C:\Windows\SysWOW64 for 32-bit or in system32 for 64-bit

Re: LUA - Script with PureBasic

Posted: Sun May 03, 2020 5:40 pm
by Joubarbe
In Lua.pbi, there is this line:

Code: Select all

#Lua_Library_File = "/Binaries/Windows/x64/lua53.lib"
My structure folder is as follow:

Code: Select all

- Libraries
  ↪ Lua
    ↪ Binaries
      ↪ Windows\x64
      lua53.dll
      lua53.lib
    Lua.pbi
Example.pb
So... To me, Lua.pbi is targeting the right location? I see no reference of a lib folder, but I'm probably missing something here :)
(Example.pb is at root, and Lua.pbi is in \Libraries\Lua)

Re: LUA - Script with PureBasic

Posted: Sun May 03, 2020 5:57 pm
by jack

Code: Select all

#Lua_Library_File = "/Binaries/Windows/x64/lua53.lib"
is obviously wrong, *.lib files go into PureLibraries\Windows\Libraries
as for this line: #Lua_Library_File = "/Binaries/Windows/x64/lua53.lib"
I think all you need is :#Lua_Library_File = "lua53.lib"

Re: LUA - Script with PureBasic

Posted: Tue May 12, 2020 1:27 pm
by FlatEarth
For the lua 5.4 version, source needs to be changed?

Re: LUA - Script with PureBasic

Posted: Tue May 12, 2020 3:07 pm
by jack
what OS and tools are you using?
for mingw on Windows all you need to do is make mingw

Re: LUA - Script with PureBasic

Posted: Tue May 12, 2020 7:29 pm
by FlatEarth
jack wrote:what OS and tools are you using?
for mingw on Windows all you need to do is make mingw
I have no problem building lua library.
I mean, do we need to change GPI codes to use the new version of lua? there is no discrepancy?

Re: LUA - Script with PureBasic

Posted: Thu May 14, 2020 1:20 pm
by DoubleDutch
If you have it all working for 5.4, can you post a link to a zip?