This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Writing multifile lua programs
Started by Stekeblad, 05 June 2016 - 07:49 AMPosted 05 June 2016 - 09:49 AM
I am thinking about trying to split my next CC program up in several files but did not find much information on this. My questions is how have you made multifile programs? How do you load in the files, load api/loadfile/… ? Other things to keep in mind then writing multifile programs compared to having everything in one?
Posted 05 June 2016 - 10:00 AM
You should first decide how you'll want to load these files. My personal preference would be to use dofile, loading files which return a table of functions. Next you'll want to use shell.getRunningProgram together with fs.getDir to find out the root directory of your program, so you could load your files relative to your program's location. And then you'll have to figure out how you'll want to distribute your program. There are plenty of programs which allow you to pack a bunch of files together so you could put the single packed file onto pastebin. Just search for them in Programs/APIs and Utilities subforum.
Posted 05 June 2016 - 10:49 AM
Personally when I split my scripts I go with APIs - and even then I only do it if I reckon I'll be re-using the resulting code in other projects.
If I'm writing code that'll only be used by one script file, I just stick it in that script file. Heck, I tend to get stroppy if I see coders making too many redundant functions, nevermind trying to split stuff off into whole other files…
I'm a lumper.
If I'm writing code that'll only be used by one script file, I just stick it in that script file. Heck, I tend to get stroppy if I see coders making too many redundant functions, nevermind trying to split stuff off into whole other files…
I'm a lumper.
Posted 06 June 2016 - 05:38 PM
Best way would be to use my make program. It's very complete with preprocessor functions and such, supports dependencies, is very customizable and outputs a single file.
http://www.computercraft.info/forums2/index.php?/topic/26048-lua-make-lua-pre-processor-for-computercraft/page__pid__246756#entry246756
http://www.computercraft.info/forums2/index.php?/topic/26048-lua-make-lua-pre-processor-for-computercraft/page__pid__246756#entry246756
Posted 06 June 2016 - 08:32 PM
You can do something like this:
My.dll:
aConf.cfg
aSubProgram.lua
local myAPI = dofile('My.dll')
local res = myAPI.makeSomething('aConf.cfg')
loadfile('aSubProgram.lua')(res)
My.dll:
API = {}
API['makeSomething'] = function(path) <CODE> end
return API
aConf.cfg
My = true
return _ENV -- Make sure _ENV._ENV is ignored in a for - pairs loops
aSubProgram.lua
local my = ...
if my then
<CODE>
end
Edited on 06 June 2016 - 06:34 PM
Posted 06 June 2016 - 08:43 PM
Personally when I split my scripts I go with APIs - and even then I only do it if I reckon I'll be re-using the resulting code in other projects.
If I'm writing code that'll only be used by one script file, I just stick it in that script file. Heck, I tend to get stroppy if I see coders making too many redundant functions, nevermind trying to split stuff off into whole other files…
For small projects I'd agree. However there has to be a limit: the ~3000 line monstrosity of Firewolf could really do with refactoring into separate modules just to keep things navigatable. My advise would be to not be over-eager to split things up. If you choose to I'd recommend several programs:
- Howl: can manually specify dependencies or just use Lua's "require" function (disclaimer: written by me).
- Compilr: Unlike most other packagers this doesn't require extracting into a separate folder: the program can be run in place.
- Package: extracts multiple files to a location. However this does support compression and base64 encoding: allowing binary files.
Edited on 06 June 2016 - 06:44 PM
Posted 07 June 2016 - 03:05 AM
For small projects I'd agree. However there has to be a limit: the ~3000 line monstrosity of Firewolf could really do with refactoring into separate modules just to keep things navigatable.
Fair enough that there's a point where things get silly, but everyone has a different idea as to where that limit should be. I was almost going to name a figure in my last post, though it had an extra zero over what you've got there.
Firewolf in particular could have its line count heavily reduced without any splitting, mind you.