Lightweight, fast, Java-centric Lua interpreter written for JME and JSE, with string, table, package, math, io, os, debug, coroutine & luajava…
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?
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.
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)
B4X:
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
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?
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.
' 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
B4X:
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
B4X:
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
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?
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.
Little extra piece of code to show how to set a variable in a class from Luaj (must be a public variable)
B4X:
' 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
B4X:
Sub Class_Globals
Private fx As JFX
Public aField As Int
End Sub