Posted 07 August 2016 - 09:55 PM
I'd like to write my own API and use it in other scripts.
This is an example of an API I want to use (all code is still work in progress):
Ideally I'd like it used something like this:
This is how you'd do it in a "normal" lua environment with "require" (which has been removed from ComputerCraft), as far as I've read. But this is not working. It says "File not found". If I change the first line like this:
One thing I'd really like to keep is the ".lua" extension on my file, because I'm using an external editor to write this code and the extension ensures my editor recognizing the file as lua code.
So, the question is… How to write things like this? Is there a recommended way to do it?
This is an example of an API I want to use (all code is still work in progress):
Log = {}
Log.__index = Log
function Log.create(fileName)
local _ = {}
setmetatable(_, Log)
_.fileName = fileName
return _
end
function Log:add(message)
local _ = io.open(self:fileName, "a")
io.output(_)
io.write(message)
io.flush()
end
function Log:addLine(message)
self:add(message + "\r\n")
end
function Log:clear()
local _ = io.open(self:fileName, "w+")
io.output(_)
io.write("")
io.flush()
end
Ideally I'd like it used something like this:
os.loadAPI("Log")
log = Log.create("main.log")
log:addLine("Hello!")
This is how you'd do it in a "normal" lua environment with "require" (which has been removed from ComputerCraft), as far as I've read. But this is not working. It says "File not found". If I change the first line like this:
os.loadAPI("Log.lua")
Then I get this error:
bios:14: [string "Log.lua"]:12: function arguments expected
One thing I'd really like to keep is the ".lua" extension on my file, because I'm using an external editor to write this code and the extension ensures my editor recognizing the file as lua code.
So, the question is… How to write things like this? Is there a recommended way to do it?