S sseaand Member Licensed User Longtime User Oct 29, 2023 #1 I've been asking about Lua for a long time, but I still haven't found a solution on how to use my old work from Lua in B4X. I recently found LuaJ, Roseborough.com Luaj - Browse Files at SourceForge.net Luaj files. Full list of files for Luaj sourceforge.net Can anyone tell me how I can run my Lua script from a file using this LuaJ virtual machine from the B4J application and if this is possible then also B4A?
I've been asking about Lua for a long time, but I still haven't found a solution on how to use my old work from Lua in B4X. I recently found LuaJ, Roseborough.com Luaj - Browse Files at SourceForge.net Luaj files. Full list of files for Luaj sourceforge.net Can anyone tell me how I can run my Lua script from a file using this LuaJ virtual machine from the B4J application and if this is possible then also B4A?
kimstudio Active Member Licensed User Longtime User Oct 31, 2023 #21 Fantasy consoles/computers like PICO-8 or TIC80 use Lua as game programming language, the performance is very good. I hope this Java Lua can also call B4X functions/procedures then we can make Fantasy console in B4X like the one I tried before using Basic script engine by Agraham. Upvote 0
Fantasy consoles/computers like PICO-8 or TIC80 use Lua as game programming language, the performance is very good. I hope this Java Lua can also call B4X functions/procedures then we can make Fantasy console in B4X like the one I tried before using Basic script engine by Agraham.
S sseaand Member Licensed User Longtime User Oct 31, 2023 #22 Daestrum said: You can also make an alias to Main.scrptEngine to make the code easier to read. (and far less typing) Click to expand... Great, I'll try it! Thank you very much for the help! Upvote 0
Daestrum said: You can also make an alias to Main.scrptEngine to make the code easier to read. (and far less typing) Click to expand... Great, I'll try it! Thank you very much for the help!
Daestrum Expert Licensed User Longtime User Nov 2, 2023 #23 Complete B4XPages example (uses Luaj 3.0.1 and only tested in B4J) in case anyone else want to try it. The Lua source file (testLua.lua) (can't add as a file to post) (put it in Objects folder) Code: function max(num1, num2) if (num1 >= num2) then result = num1; else result = num2; end return result; end function min(num1, num2) if (num1 <= num2) then result = num1; else result = num2; end return result; end Edit : ammended attached project. Attachments Project.zip Project.zip 10.1 KB · Views: 390 Last edited: Nov 2, 2023 Upvote 0
Complete B4XPages example (uses Luaj 3.0.1 and only tested in B4J) in case anyone else want to try it. The Lua source file (testLua.lua) (can't add as a file to post) (put it in Objects folder) Code: function max(num1, num2) if (num1 >= num2) then result = num1; else result = num2; end return result; end function min(num1, num2) if (num1 <= num2) then result = num1; else result = num2; end return result; end Edit : ammended attached project.
Daestrum Expert Licensed User Longtime User Nov 2, 2023 #24 Amended code in the above post attachment to avoid pattern matching switch statement. Last edited: Nov 2, 2023 Upvote 0
S sseaand Member Licensed User Longtime User Nov 2, 2023 #25 Daestrum said: Added code to the above post to avoid pattern matching switch statement. Click to expand... Maybe you can edit the archive attachment to avoid confusion? Upvote 0
Daestrum said: Added code to the above post to avoid pattern matching switch statement. Click to expand... Maybe you can edit the archive attachment to avoid confusion?
kimstudio Active Member Licensed User Longtime User Nov 2, 2023 #26 Thanks @Daestrum for the example, which is B4X calls Lua functions. Could Lua call B4X functions? Upvote 0
aeric Expert Licensed User Longtime User Nov 2, 2023 #27 kimstudio said: Could Lua call B4X functions? Click to expand... You should start a new thread. If you are asking programming language X can call b4X functions, it is similar to asking : Can Java call B4X function? Can PHP call B4X function? Can Python call B4X function? Can C# call B4X function? the list goes on... B4X is powerful to use inline code to call functions or class from other programming languages BUT other languages are not as powerful as B4X. You may want to ask their developers whether they support different languages like B4X? Upvote 0
kimstudio said: Could Lua call B4X functions? Click to expand... You should start a new thread. If you are asking programming language X can call b4X functions, it is similar to asking : Can Java call B4X function? Can PHP call B4X function? Can Python call B4X function? Can C# call B4X function? the list goes on... B4X is powerful to use inline code to call functions or class from other programming languages BUT other languages are not as powerful as B4X. You may want to ask their developers whether they support different languages like B4X?
Daestrum Expert Licensed User Longtime User Nov 2, 2023 #28 kimstudio said: Could Lua call B4X functions Click to expand... I will try to get it to call a B4X sub (no promises it will work) Upvote 0
kimstudio said: Could Lua call B4X functions Click to expand... I will try to get it to call a B4X sub (no promises it will work)
S sseaand Member Licensed User Longtime User Nov 2, 2023 #29 I found a later version 3.0.2 released by another person who continued the LuaJ project, perhaps some bugs were fixed there. If anyone is interested in using it, I'll leave a link. Releases · luaj/luaj Lightweight, fast, Java-centric Lua interpreter written for JME and JSE, with string, table, package, math, io, os, debug, coroutine & luajava libraries, JSR-223 bindings, all metatags, weak ta... github.com Upvote 0
I found a later version 3.0.2 released by another person who continued the LuaJ project, perhaps some bugs were fixed there. If anyone is interested in using it, I'll leave a link. Releases · luaj/luaj Lightweight, fast, Java-centric Lua interpreter written for JME and JSE, with string, table, package, math, io, os, debug, coroutine & luajava libraries, JSR-223 bindings, all metatags, weak ta... github.com
Daestrum Expert Licensed User Longtime User Nov 2, 2023 #30 code to call a sub from luaj Code: ' calling a sub in a class from lua Dim luaFuncCall As JavaObject luaFuncCall = se.get_Function("callSub",bindings) ' callSub is in lua script Dim luaargs As JavaObject 'ignore Dim xtra As luaExtras xtra.Initialize luaargs = se.createArgs(Array(xtra)) se.callLua(luaFuncCall,luaargs) The modified lua script Code: function max(num1, num2) if (num1 >= num2) then result = num1; else result = num2; end return result; end function min(num1, num2) if (num1 <= num2) then result = num1; else result = num2; end return result; end function callSub(clss) clss:_testcallfromlua(clss) return nil end and finally the luaExtras class Code: Sub Class_Globals Private fx As JFX End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize End Sub Sub testCallFromLua() Log("I was called from Luaj") End Sub Upvote 0
code to call a sub from luaj Code: ' calling a sub in a class from lua Dim luaFuncCall As JavaObject luaFuncCall = se.get_Function("callSub",bindings) ' callSub is in lua script Dim luaargs As JavaObject 'ignore Dim xtra As luaExtras xtra.Initialize luaargs = se.createArgs(Array(xtra)) se.callLua(luaFuncCall,luaargs) The modified lua script Code: function max(num1, num2) if (num1 >= num2) then result = num1; else result = num2; end return result; end function min(num1, num2) if (num1 <= num2) then result = num1; else result = num2; end return result; end function callSub(clss) clss:_testcallfromlua(clss) return nil end and finally the luaExtras class Code: Sub Class_Globals Private fx As JFX End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize End Sub Sub testCallFromLua() Log("I was called from Luaj") End Sub
kimstudio Active Member Licensed User Longtime User Nov 3, 2023 #31 aeric said: B4X is powerful to use inline code to call functions or class from other programming languages BUT other languages are not as powerful as B4X. You may want to ask their developers whether they support different languages like B4X? Click to expand... aeric, this is under different context. If another programming languages can be embedded into B4X as a script engine then it should have capability to call B4X functions/access B4X variables to make it useful. Upvote 0
aeric said: B4X is powerful to use inline code to call functions or class from other programming languages BUT other languages are not as powerful as B4X. You may want to ask their developers whether they support different languages like B4X? Click to expand... aeric, this is under different context. If another programming languages can be embedded into B4X as a script engine then it should have capability to call B4X functions/access B4X variables to make it useful.
Daestrum Expert Licensed User Longtime User Nov 3, 2023 #32 Little extra piece of code to show how to set a variable in a class from Luaj (must be a public variable) Code: ' set a B4X field in a class from lua Dim luaFuncCall As JavaObject luaFuncCall = se.get_Function("setB4XVariable",bindings) 'function in lua script see below ' *** the Lua function *** in the file ' function setB4XVariable(clss) ' clss._afield = 23 ' Return nil ' End luaargs = se.createArgs(Array(xtra)) ' pass the object se.callLua(luaFuncCall,luaargs) ' call the function Log(xtra.aField) ' print what B4X sees (Value was zero before call) The field in LuaExtras class Code: Sub Class_Globals Private fx As JFX Public aField As Int End Sub Upvote 0
Little extra piece of code to show how to set a variable in a class from Luaj (must be a public variable) Code: ' set a B4X field in a class from lua Dim luaFuncCall As JavaObject luaFuncCall = se.get_Function("setB4XVariable",bindings) 'function in lua script see below ' *** the Lua function *** in the file ' function setB4XVariable(clss) ' clss._afield = 23 ' Return nil ' End luaargs = se.createArgs(Array(xtra)) ' pass the object se.callLua(luaFuncCall,luaargs) ' call the function Log(xtra.aField) ' print what B4X sees (Value was zero before call) The field in LuaExtras class Code: Sub Class_Globals Private fx As JFX Public aField As Int End Sub