This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tommykent1210's profile picture

Including LUA Libraries?

Started by tommykent1210, 29 July 2012 - 08:08 PM
tommykent1210 #1
Posted 29 July 2012 - 10:08 PM
I'm fairly new to LUA, but have a fair bit of PHP/vb.net/java experience so LUA came pretty quickly to me. However I'm not entirely sure how to do one thing - libraries.

Now, I understand CC uses it's "APIs" that work kind of like libraries, but i want a SPECIFIC lua library in CC :ph34r:/>/>


Basically, I want http://viremo.eludi.net/LuaXML/ so that I can easily parse XML files (config files for the OS I'm writing).

Normally, you would get this to work by just putting:


require("LuaXml")

and it would work. However, as I understand it, require() was removed from CC.


So,
a ) where do I put the library (which file should I even use from the download?)
b ) how do I get it to work? I was hoping to just drop one of the library files into the APIs folder and be working but life doesnt seem to be that simple :)/>/>




Thanks
MysticT #2
Posted 29 July 2012 - 10:17 PM
Sorry, you can't use C libraries in CC. You can only load apis writen in pure lua, using the os.loadAPI function. In the api file you write all the functions you want to include in the api as global, and the function loads them into a table stored as a global with the filename as name (ie, you load the MyApi file and the table will be called MyApi).
tommykent1210 #3
Posted 29 July 2012 - 10:19 PM
nawwww, so no XML handling? :ph34r:/>/>
MysticT #4
Posted 29 July 2012 - 10:22 PM
Yes, but not with that library. Just search for one in pure lua, or write one yourself.
tommykent1210 #5
Posted 29 July 2012 - 10:24 PM
ok, with this one: http://www.hpelbers.org/lua/xl

What do I do about the XL = {} (which I understand to be LUA's way of attempting classes)?
Lyqyd #6
Posted 29 July 2012 - 10:45 PM
That'll need some slight modifications, but it should work. Calling it will be a little strange, though. I think it'd be a bit more like parser = xl.XL:new(), but you could rearrange things in there to work around that. Take a look at the vector API if you're interested in making it prettier for use with ComputerCraft.
MysticT #7
Posted 29 July 2012 - 10:47 PM
You can use that one. You have to save it to a file, and then use os.loadAPI to load it as an api, and then use it like:

local parser = <apiName>.XL:new()
parser:from_file("SomeXMLFile")
local a = parser:find(parser:root(), 'A')
Where apiName is the name of the file.
tommykent1210 #8
Posted 30 July 2012 - 12:45 AM
Ok thanks. Could I just drop it into the APIs folder and skip the "loadAPI" part?
Lyqyd #9
Posted 30 July 2012 - 01:07 AM
Affirmative.