B4J Question Question about using third-party software

sseaand

Member
Licensed User
Longtime User
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,

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
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

Daestrum

Expert
Licensed User
Longtime User
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

Edit : ammended attached project.
 

Attachments

  • Project.zip
    10.1 KB · Views: 126
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Amended code in the above post attachment to avoid pattern matching switch statement.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Could Lua call B4X functions?
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

sseaand

Member
Licensed User
Longtime User
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.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
code to call a sub from luaj
B4X:
    ' 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
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
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.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
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
 
Upvote 0
Top