XMedders Posted December 22, 2015 Share Posted December 22, 2015 This tutorial is dedicated to shawnmick23, as he wanted to see the bridge from C's include directive to ComputerCraft's os.loadAPI function. Even though I don't know a whole lot of C, I know a whole lot of lua . API's can be used for many different things! The most common uses are Effecient distribution of code packages, Efficient storing of functions, etc. So, today We will be learning one of the most useful things in the os api for computercraft. This is the function os.loadAPI() The function os.loadAPI takes a parameter of a string which is the filename. e.g. os.loadAPI("filenamehere") Once you call this, all of the functions in that file will from then on be available from inside the file that called it. However, whenever you edit the api, you will need to call it again for the changes to take place. The best way to do this is to call the loading of the api in the first line of the program that you're editing. An Example api would be a file named ccolorapi - Custom Color Api! -- This is an api for blah blah blah blah. -- made by your name here, please give credit! apihelp = { --custom defined help dictionary, much like peripheral.getMethods() --function name, function desc {"clearColor","Makes the background color the specified color"}, {"setColorVars","Sets all of the variables that have to do with colors"} } function listMethods() for i = 1,#apihelp-1 do print(apihelp[i][1]..",") end print(apihelp[#apihelp][1]..".") end function help(func) for i = 1,#apihelp do if apihelp[i][1] == func then return apihelp[i][2] end end end function clearColor(color) -- I can see this function being kinda useful term.setBackgroundColor(color) term.clear() term.setCursorPos(1,1) end function setColorVars(tcolor,bcolor) -- this one, not so much term.setBackgroundColor(bcolor) term.srtTextColor(tcolor) end Then, to reference this api, we would do something along the lines of this os.loadAPI("ccolorapi") clearColor(colors.lime) -- makes the background lime print("HELLO WORLD :D") If you wanted to see the list of functions or the description of a function in the lua prompt or somehting, you would type in the prompt os.loadAPI("ccolorapi") ccolorapi.listMethods() ccolorapi.help("setColorVars") And that's all there is to it, folks Thanks-a-million -XMedders Powerwarp 1 Quote Link to comment Share on other sites More sharing options...
Conspiracy Posted December 22, 2015 Share Posted December 22, 2015 Wow man, you really get material right? Nice work! (shouldn't have time to read this thread now, but I will do that later, when I will wake up). XMedders 1 Quote Link to comment Share on other sites More sharing options...
XMedders Posted December 22, 2015 Author Share Posted December 22, 2015 Wow man, you really get material right? Nice work! (shouldn't have time to read this thread not, but I will later, when I will wake up). Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.