LUA - Script with PureBasic

Share your advanced PureBasic knowledge/code with the community.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

LUA - Script with PureBasic

Post 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.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post 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
:)
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: LUA - Script with PureBasic

Post 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 :-)
Last edited by Xanos on Tue May 09, 2017 8:37 am, edited 1 time in total.
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
happer66
User
User
Posts: 33
Joined: Tue Jan 12, 2010 12:10 pm
Location: Sweden

Re: LUA - Script with PureBasic

Post 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!
Image If your code isn't clean atleast make sure it's pure!
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post by jack »

happer66 wrote: Thanks for the module GDI!
was that spelling auto-correct ?
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Re: LUA - Script with PureBasic

Post by superadnim »

link is broken?

:lol: should I bash the keyboard and give up?
:?
User avatar
GG
Enthusiast
Enthusiast
Posts: 258
Joined: Tue Jul 26, 2005 12:02 pm
Location: Lieusaint (77), France

Re: LUA - Script with PureBasic

Post by GG »

Purebasic 6.04 64 bits - Windows 11 Pro 64 bits 23H2
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: LUA - Script with PureBasic

Post 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
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post 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
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: LUA - Script with PureBasic

Post 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)
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post 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"
FlatEarth

Re: LUA - Script with PureBasic

Post by FlatEarth »

For the lua 5.4 version, source needs to be changed?
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post by jack »

what OS and tools are you using?
for mingw on Windows all you need to do is make mingw
FlatEarth

Re: LUA - Script with PureBasic

Post 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?
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: LUA - Script with PureBasic

Post by DoubleDutch »

If you have it all working for 5.4, can you post a link to a zip?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply