How can I use require() in a script?
Moderators: Víctor Paredes, Belgarath, slowtiger
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
How can I use require() in a script?
Hello, I have seen many useful libraries, but I don't know if I could install them in a lua script and use the require() function.
Would anyone know how to install those libraries and then use them in a script ?
These libraries can be downloaded from github.com .
Would anyone know how to install those libraries and then use them in a script ?
These libraries can be downloaded from github.com .
Re: How can I use require() in a script?
I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:
Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.
Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.
For ref:: https://gist.github.com/tylerneylon/59f ... be525b30ab -- a nice & compact pure Lua JSON lib.
Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.
Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.
Code: Select all
local json
function XX_My_Script:IsRelevant(moho)
if (not _did_my_resources_package_path) then
package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;"
_did_my_resources_package_path = true
end
json = json or require("json")
return true
end
Last edited by SimplSam on Thu Apr 06, 2023 2:07 pm, edited 1 time in total.
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: How can I use require() in a script?
Thank you.SimplSam wrote: ↑Wed Apr 05, 2023 6:26 am I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:
Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.
Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.
Code: Select all
local json function XX_My_Script:IsRelevant(moho) if (not _did_my_resources_package_path) then package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;" _did_my_resources_package_path = true end json = json or require("json") return true end
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: How can I use require() in a script?
Hello, a question.SimplSam wrote: ↑Wed Apr 05, 2023 6:26 am I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:
Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.
Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.
For ref:: https://gist.github.com/tylerneylon/59f ... be525b30ab -- a nice & compact pure Lua JSON lib.Code: Select all
local json function XX_My_Script:IsRelevant(moho) if (not _did_my_resources_package_path) then package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;" _did_my_resources_package_path = true end json = json or require("json") return true end
Could you tell me in json there is a function that is "decode" when I call require("json")
but when I call json.decode(result) there is an error, why is that?
Re: How can I use require() in a script?
It depends which library you were using, and also what the error was.bbrraayyaann wrote: ↑Sun Apr 09, 2023 6:41 pm ... Could you tell me in json there is a function that is "decode" when I call require("json"), but when I call json.decode(result) there is an error, why is that?
If it was the same library as the one I referenced, then it has 2 primary functions.
myTable = json.parse(myString) - to convert a JSON text string into a Lua table (encode)
myString = json.stringify(myTable) - to convert a Lua table into a JSON text string (decode)
* The Lua table acts as the JSON object.
Last edited by SimplSam on Tue Apr 18, 2023 5:14 am, edited 1 time in total.
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: How can I use require() in a script?
Hi SimplSam, thanks for the answer.SimplSam wrote: ↑Sun Apr 09, 2023 7:58 pmIt depends which library you were using, and also what the error was.bbrraayyaann wrote: ↑Sun Apr 09, 2023 6:41 pm ... Could you tell me in json there is a function that is "decode" when I call require("json"), but when I call json.decode(result) there is an error, why is that?
If it was the same library as the one I referenced, then it has 2 primary functions.
myTable = json.parse(myString) - to convert a JSON text string into a Lua table (encode)
myString = json.stringyfy(myTable) - to convert a Lua table into a JSON text string (decode)
* The Lua table acts as the JSON object.
Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
Re: How can I use require() in a script?
This has been tried before, and unfortunately the current conclusion is binary libraries do not work with Moho. There are 2 primary reasons:bbrraayyaann wrote: ↑Tue Apr 11, 2023 3:23 pm ... Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
1) Binary compatibility
Whilst most generic Lua script code can run on any version of Lua with few modifications; The ABI (Application Binary Interface) of Lua major.minor versions are not compatible. i.e. Lua 5.2.xx is not binary compatible with 5.1.xx nor 5.4.xx. etc. So a binary plugin built for 5.1 would not run with a 5.2 host.
You could potentially get around this issue by building your binary client library to the correct matching Lua version.
2) Duplicate virtual machines
Current Moho runs Lua version 5.2, with processes in a virtual machine of a non-shared binary library. Which means add-on client libraries would need to run in another Lua virtual machine instance. Unfortunately Lua 5.2 does not allow what it calls 'duplicate virtual machines', thus you cannot run the host & client binaries simultaneously - as parent & child.
It order to fix this, Moho would need to either use shared Lua libraries or Moho & Client upgrade to Lua 5.4+.
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: How can I use require() in a script?
I see, I have investigated and what can be done is to compile the library to be portable to pure Lua code.SimplSam wrote: ↑Thu Apr 13, 2023 4:43 am...bbrraayyaann wrote: ↑Tue Apr 11, 2023 3:23 pm ... Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
And yes it is possible but you need advanced knowledge in C.
Another option I looked into and it worked for me, was to write a code similar to the library you want in Python language and then compile it into a .exe file.
Then run io.popen() in lua with the .exe file you created in Python.
And that works just pass it as an argument what you want in lua using io.popen and it will run as an external file using Python.