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

How do I create an API?

Started by trajing, 28 January 2014 - 11:15 AM
trajing #1
Posted 28 January 2014 - 12:15 PM
I have an idea for an API and I have a good Idea of the functions, but I don't know how to define that it's a new API. Is there some sort of function I use or do I just create a table?
TechMasterGeneral #2
Posted 28 January 2014 - 12:21 PM
My understanding of that is(i've never made one) that you write the code and then use the os.loadAPI("path/to/file") to load your code as an api… if your asking how to make one then… sorry i can't help you there.
surferpup #3
Posted 28 January 2014 - 12:37 PM
Here are some resources for you:They are not difficult to build, and they can be quite useful. Give it a try.
Edited on 28 January 2014 - 11:38 AM
ingie #4
Posted 28 January 2014 - 12:43 PM
I have an idea for an API and I have a good Idea of the functions, but I don't know how to define that it's a new API. Is there some sort of function I use or do I just create a table?

if you already have the functions (say, in a test program) then you just need to move those functions into a new file, and load that file with the os.loadAPI call mentioned above

if you call your "file-o-functions" something like "fred" then

os.loadAPI("fred")

will then allow

fred.someFunctionInYourFredFile( params ) to be used

that's the basics which will allow what you have to be used… remember that in the api file, you can have local functions and non-local functions, if you define one as local then it will be internal to the api only (for example, a helper function that only the api needs)
remember to keep any variables in your api file local where possible…

more details in the above replies of course, but related to your question, and assuming you have some functions already, those two steps should bootstrap you quickly
surferpup #5
Posted 28 January 2014 - 12:51 PM
I also learned the hard way that you need to take care of the order of any local functions you create. A local function in an API is accessible by the code in the API, but it is not callable by the user (kind of handy, really). However, if one of your local functions relies on another local function in the API, the local function it needs MUST be defined before you call it.

Here is the question I had asked on this and the excellent answers I received: [url=http://www.computercraft.info/forums2/index.php?/topic/16853-question-regarding-local-functions-in-apis']Question Regarding Local Functions in APIs[/url]